首頁 >後端開發 >php教程 >重组数组 - PHP 重组二维数组

重组数组 - PHP 重组二维数组

WBOY
WBOY原創
2016-06-06 20:48:211277瀏覽

如何将原二维数组重组成以下形式?

原数组

<code>Array
(
[0] => Array
    (
        [第1-2节] => 
        [第3-4节] => Web网站开发与维护_每周_XXXX_01-15周_S1-205
        [第5-6节] => Linux高级应用_每周_XXXX_01-15周_A-910
        [第7-8节] => 网络安全_每双周_XXXX_01-15周_A-507
        [第9-10节] => 
    )

[1] => Array
    (
        [第1-2节] => IPV6技术_每周_XXXX_01-14周_S1-303
        [第3-4节] => 
        [第5-6节] => 网络安全_每周_XXXX_01-15周_C-408
        [第7-8节] => 
        [第9-10节] => 
    )

[2] => Array
    (
        [第1-2节] => 网络营销_每周_XXXX_01-14周_C-201
        [第3-4节] => 
        [第5-6节] => 就业指导_每周_XXXX_11-12周_B-202
        [第7-8节] => 
        [第9-10节] => 
    )

[3] => Array
    (
        [第1-2节] => Linux高级应用_每单周_XXXX_01-15周_A-910/Web网站开发与维护_每双周_张劲波_01-15周_A-910
        [第3-4节] => 
        [第5-6节] => Cisco高级应用_每周_XXXX_01-14周_S1-305
        [第7-8节] => Linux高级应用_每周_XXXX_01-15周_S1-303
        [第9-10节] => 
    )

[4] => Array
    (
        [第1-2节] => 网络安全_每周_XXXX_01-15周_A-508
        [第3-4节] => Web网站开发与维护_每周_XXXX_01-15周_A-908
        [第5-6节] => 
        [第7-8节] => 
        [第9-10节] => 
    )

)
</code>

重组后的格式为:

<code>Array
(
[0] => Array
    (
        [第1-2节] => 
        [第1-2节] => IPV6技术_每周_XXXX_01-14周_S1-303
        [第1-2节] => 网络营销_每周_XXXX_01-14周_C-201
        [第1-2节] => Linux高级应用_每单周_XXXX_01-15周_A-910/Web网站开发与维护_每双周_XXXX_01-15周_A-910
        [第1-2节] => 网络安全_每周_XXXX_01-15周_A-508
    )

[1] => Array
    (
        [第3-4节] => Web网站开发与维护_每周_XXXX_01-15周_S1-205
        [第3-4节] => 
        [第3-4节] => 
        [第3-4节] => 
        [第3-4节] => Web网站开发与维护_每周_XXXX_01-15周_A-908
    )

[2] => Array
    (
        [第5-6节] => Linux高级应用_每周_XXXX_01-15周_A-910
        [第5-6节] => 网络安全_每周_XXXX_01-15周_C-408
        [第5-6节] => 就业指导_每周_XXXX_11-12周_B-202
        [第5-6节] => Cisco高级应用_每周_XXXX_01-14周_S1-305
        [第5-6节] => 
    )

[3] => Array
    (
        [第7-8节] => 网络安全_每双周_XXXX_01-15周_A-507
        [第7-8节] => 
        [第7-8节] => 
        [第7-8节] => Linux高级应用_每周_XXXX_01-15周_S1-303
        [第7-8节] => 
    )

[4] => Array
    (
        [第9-10节] => 
        [第9-10节] => 
        [第9-10节] => 
        [第9-10节] => 
        [第9-10节] => 
    )

)
</code>

回复内容:

如何将原二维数组重组成以下形式?

原数组

<code>Array
(
[0] => Array
    (
        [第1-2节] => 
        [第3-4节] => Web网站开发与维护_每周_XXXX_01-15周_S1-205
        [第5-6节] => Linux高级应用_每周_XXXX_01-15周_A-910
        [第7-8节] => 网络安全_每双周_XXXX_01-15周_A-507
        [第9-10节] => 
    )

[1] => Array
    (
        [第1-2节] => IPV6技术_每周_XXXX_01-14周_S1-303
        [第3-4节] => 
        [第5-6节] => 网络安全_每周_XXXX_01-15周_C-408
        [第7-8节] => 
        [第9-10节] => 
    )

[2] => Array
    (
        [第1-2节] => 网络营销_每周_XXXX_01-14周_C-201
        [第3-4节] => 
        [第5-6节] => 就业指导_每周_XXXX_11-12周_B-202
        [第7-8节] => 
        [第9-10节] => 
    )

[3] => Array
    (
        [第1-2节] => Linux高级应用_每单周_XXXX_01-15周_A-910/Web网站开发与维护_每双周_张劲波_01-15周_A-910
        [第3-4节] => 
        [第5-6节] => Cisco高级应用_每周_XXXX_01-14周_S1-305
        [第7-8节] => Linux高级应用_每周_XXXX_01-15周_S1-303
        [第9-10节] => 
    )

[4] => Array
    (
        [第1-2节] => 网络安全_每周_XXXX_01-15周_A-508
        [第3-4节] => Web网站开发与维护_每周_XXXX_01-15周_A-908
        [第5-6节] => 
        [第7-8节] => 
        [第9-10节] => 
    )

)
</code>

重组后的格式为:

<code>Array
(
[0] => Array
    (
        [第1-2节] => 
        [第1-2节] => IPV6技术_每周_XXXX_01-14周_S1-303
        [第1-2节] => 网络营销_每周_XXXX_01-14周_C-201
        [第1-2节] => Linux高级应用_每单周_XXXX_01-15周_A-910/Web网站开发与维护_每双周_XXXX_01-15周_A-910
        [第1-2节] => 网络安全_每周_XXXX_01-15周_A-508
    )

[1] => Array
    (
        [第3-4节] => Web网站开发与维护_每周_XXXX_01-15周_S1-205
        [第3-4节] => 
        [第3-4节] => 
        [第3-4节] => 
        [第3-4节] => Web网站开发与维护_每周_XXXX_01-15周_A-908
    )

[2] => Array
    (
        [第5-6节] => Linux高级应用_每周_XXXX_01-15周_A-910
        [第5-6节] => 网络安全_每周_XXXX_01-15周_C-408
        [第5-6节] => 就业指导_每周_XXXX_11-12周_B-202
        [第5-6节] => Cisco高级应用_每周_XXXX_01-14周_S1-305
        [第5-6节] => 
    )

[3] => Array
    (
        [第7-8节] => 网络安全_每双周_XXXX_01-15周_A-507
        [第7-8节] => 
        [第7-8节] => 
        [第7-8节] => Linux高级应用_每周_XXXX_01-15周_S1-303
        [第7-8节] => 
    )

[4] => Array
    (
        [第9-10节] => 
        [第9-10节] => 
        [第9-10节] => 
        [第9-10节] => 
        [第9-10节] => 
    )

)
</code>

<code class="lang-php">[1] => Array
    (
        [第3-4节] => Web网站开发与维护_每周_XXXX_01-15周_S1-205
        [第3-4节] => 
        [第3-4节] => 
        [第3-4节] => 
        [第3-4节] => Web网站开发与维护_每周_XXXX_01-15周_A-908
    )
</code>

这种key相同的写法,后面的key会覆盖前面的,你应该是期望得到这种结果:

<code class="lang-php">[第1-2节] => Array
    (
        [0] => 
        [1] => IPV6技术_每周_XXXX_01-14周_S1-303
        [2] => 网络营销_每周_XXXX_01-14周_C-201
        [3] => Linux高级应用_每单周_XXXX_01-15周_A-910/Web网站开发与维护_每双周_XXXX_01-15周_A-910
        [4] => 网络安全_每周_XXXX_01-15周_A-508
    )

[第3-4节] => Array
    (
        [0] => Web网站开发与维护_每周_XXXX_01-15周_S1-205
        [1] => 
        [2] => 
        [3] => 
        [4] => Web网站开发与维护_每周_XXXX_01-15周_A-908
    )
</code>

可以写一个function:

<code class="lang-php">    public function listData($arr){
        if(empty($arr)) return array();
        $listData = array();
        foreach($arr as $key=>$value){
            foreach($value as $k=>$v){
                $listData[$k][] = $v;
            }
        }
        return $listData;
    }
</code>

测试数组

<code class="lang-php">$arr  = array(
    0=>array(
        'a'=>1,
        'b'=>2,
        'c'=>3,
    ),
    1=>array(
        'a'=>11,
        'b'=>22,
        'c'=>33,
    ),
    2=>array(
        'a'=>111,
        'b'=>222,
        'c'=>333,
    ),
);
</code>

得到:

<code class="lang-php">array
  'a' => 
    array
      0 => int 1
      1 => int 11
      2 => int 111
  'b' => 
    array
      0 => int 2
      1 => int 22
      2 => int 222
  'c' => 
    array
      0 => int 3
      1 => int 33
      2 => int 333
</code>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn