当前位置:Gxlcms > PHP教程 > 重组数组-PHP重组二维数组

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

时间:2021-07-01 10:21:17 帮助过:57人阅读

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

原数组

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节] => 
    )

)

重组后的格式为:

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节] => 
    )

)

回复内容:

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

原数组

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节] => 
    )

)

重组后的格式为:

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节] => 
    )

)

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

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

[第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
    )

可以写一个function:

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

测试数组

$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,
    ),
);

得到:

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

人气教程排行