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 ...