Home  >  Article  >  Backend Development  >  PHP code to insert an element into a two-dimensional array in reverse order_PHP tutorial

PHP code to insert an element into a two-dimensional array in reverse order_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:18:10914browse

复制代码 代码如下:

/**
* Insert an element into a two-dimensional array in reverse order
*
* @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);

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/325591.htmlTechArticle复制代码 代码如下: ?php /*** Insert an element into a two-dimensional array in reverse order * * @author WadeYu * @date 2012-05-30*/ $aSorted = array( array(1, 100), array(2, 90), array(3, 80), array(...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn