Inserting an element into an array in PHP program code _php Tutorial

Source: Internet
Author: User
This article is a well-ordered two-dimensional data, we want to insert an element into the array this will need to be sorted after saving to the data.
The code is as follows Copy Code

/**
* Inverted two-dimensional array inserts an element
*
* @author Wadeyu
* @date 2012-05-30
*/
$aSorted = Array (
Array (1, 100),
Array (2, 90),
Array (3, 80),
Array (4, 70),
Array (5, 60),
Array (6, 50),
Array (7, 40),
Array (8, 40),
Array (9, 40),
Array (10, 20),
);
$aInsert = Array (11, 40);
$MAXCMPIDX = 0;
$cnt = 0;
$maxCnt = 10;
foreach ($aSorted as $idx = = $arr) {
if ($arr [0] = = $aInsert [0]) {
$MAXCMPIDX = $idx;
}
$cnt + +;
}
if (! $maxCmpIdx) {
$MAXCMPIDX = $cnt + +;
}
$aSorted [$MAXCMPIDX] = $aInsert;
for ($i = $MAXCMPIDX; $i > 0; $i--) {
if ($aSorted [$i][1] > $aSorted [$i -1][1]) {
$ATMP = $aSorted [$i-1];
$aSorted [$i-1] = $aSorted [$i];
$aSorted [$i] = $ATMP;
Continue;
}
Break
}
for ($i = $cnt; $i > $maxCnt; $i-) {
Unset ($aSorted [$i-1]);
}
Print_r ($aSorted);

There are many ways to insert elements in an array, here is only one, there is a need to know more friends can go to this site search.

http://www.bkjia.com/PHPjc/631617.html www.bkjia.com true http://www.bkjia.com/PHPjc/631617.html techarticle This article is a well-ordered two-dimensional data, we want to insert an element into the array this will need to be sorted after saving to the data. Code to copy code like this? PHP ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.