逆序數組插入元素PHP實現_PHP教程

來源:互聯網
上載者:User
PHP代碼如下:


1 2 /**
3 * 逆序二維數組插入一元素
4 *
5 * @author WadeYu
6 * @date 2012-05-30
7 */
8 $aSorted = array(
9 array(1, 100),
10 array(2, 90),
11 array(3, 80),
12 array(4, 70),
13 array(5, 60),
14 array(6, 50),
15 array(7, 40),
16 array(8, 40),
17 array(9, 40),
18 array(10, 20),
19 );
20 $aInsert = array(11, 40);
21 $maxCmpIdx = 0;
22 $cnt = 0;
23 $maxCnt = 10;
24 foreach ($aSorted as $idx => $arr){
25 if ($arr[0] == $aInsert[0]){
26 $maxCmpIdx = $idx;
27 }
28 $cnt++;
29 }
30 if ( !$maxCmpIdx){
31 $maxCmpIdx = $cnt++;
32 }
33 $aSorted[$maxCmpIdx] = $aInsert;
34 for ($i = $maxCmpIdx; $i > 0; $i--){
35 if ($aSorted[$i][1] > $aSorted[$i-1][1]){
36 $aTmp = $aSorted[$i-1];
37 $aSorted[$i-1] = $aSorted[$i];
38 $aSorted[$i] = $aTmp;
39 continue ;
40 }
41 break;
42 }
43 for ($i = $cnt; $i > $maxCnt; $i--){
44 unset($aSorted[$i-1]);
45 }
46 print_r($aSorted);

摘自 huan & ping

http://www.bkjia.com/PHPjc/478176.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478176.htmlTechArticlePHP代碼如下: 1 ?php 2 /** 3 * 逆序二維數組插入一元素 4 * 5 * @author WadeYu 6 * @date 2012-05-30 7 */ 8 $aSorted = array( 9 array(1, 100), 10 array(2, 90), 11 array(...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.