php二維數組的合并2種方法

來源:互聯網
上載者:User

例1

自訂方法合并數組

先看一個二維數組:

 代碼如下 複製代碼

<?php
Array (

          [0] => Array (

                              [A] => store_name

                              [B] => 商店一

                             )

          [1] => Array (

                              [A] => store_owner

                              [B] => 小風

                              )

          [2] => Array (

                              [A] => store_name

                              [B] => 商店二

                              )

          [3] => Array (

                               [A] => store_owner

                               [B] => 小磊

                               )

?>

大家會發現裡面有兩個 store_name 和 store_owner ,所以我想讓他們合并成下面這樣

<?php 

Array (

     [0] => Array

         (

           [A] => 商店一

           [B] => 小風

          )

      [1] => Array

         (

           [A] => 商店二

           [B] => 小磊

          )

?>

合并方法如下:

 代碼如下 複製代碼

<?php
$stores = array(); //定義一個空數組
        $store_count=count($showinfo);  //統計顯示的次數,這裡的$showinfo是我擷取資料庫內容時的一個變數,然後列印出來就是上面剛開始的樣子
        for($i=0;$i<$store_count;$i++)
        {
            $j=$i+1;     //將 $i 加 1 跳過 一級  相當於 $j 取得是 索引值為 奇數的值
            $stores[]=array(
                          'name' =>$showinfo[$i]['value'],
                          'owner'=>$showinfo[$j]['value'],
                          );
            $i = $j;  //  這裡的作用是 相當於 $i 取得是 索引值為 偶數的值
        }

?>

這樣就可以得到上面的結果了!

為了讓大家看的更清楚寫 我把資料庫表結構寫出來 主要欄位如下:

key value

store_name 網店一

store_owner 小風

store_name 網店二

store_owner 小磊

 

通過上面 的方法做過後 前台頁面就可以以一排的方式顯示了,如下

name owner

網店一 小風

網店二 小磊

例3

 代碼如下 複製代碼

<?php
$arr = array
(
    0 => array(
    '1@01,02',
    '2@01,02',
    '4@ALL',
    '3@01',
    '5@01,02,04',
    ),

    1 => array(
    '1@01,02,03',
    '2@01,02,04',
    '3@ALL',
    '4@01,02',
    '111@01,05',
    '5@03',
    ),
    2 => array(
    '1@01,02,03',
    '2@02,03,05',
    '3@ALL',
    '4@01,02,03',
    '111@01,05',
    '5@03',
    ),
);
$result = array();
foreach($arr as $items){
    if(is_array($items)){
        foreach($items as $item){
            $item = explode('@', $item);
            if(count($item) != 2){
                continue ;
            }
            $result[$item[0]] .= $item[1].',';
        }
    }
}
function reJoin(&$item,$key,$seq){
    $list = array_unique(explode($seq,$item));
    if (in_array('ALL', $list)){
        $item = $key.'@ALL';
    }else{
        $item = $key.'@'.join($seq,$list);
    }
}
array_walk($result, 'reJoin',',');
sort($result);
var_export($result);
/**
 * array (
  * 0 => '111@01,05,',
  * 1 => '1@01,02,03,',
  * 2 => '2@01,02,04,03,05,',
  * 3 => '3@ALL',
  * 4 => '4@ALL',
  * 5 => '5@01,02,04,03,',
  * )
 */
?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.