Home >Backend Development >PHP Tutorial >重组数组 - PHP 重组二维数组

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

WBOY
WBOYOriginal
2016-06-06 20:48:211270browse

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

原数组

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