如何将array_map / array_walk函数用于多维数组

时间:2014-06-18 12:34:58

标签: php arrays multidimensional-array

我有以下多维数组:

array (size=2)
      10 => 
        array (size=7)
          1 => 
            array (size=4)
              0 => string '10view1' (length=7)
              1 => string '10ins1' (length=6)
              2 => string '10edit1' (length=7)
              3 => string '10del1' (length=6)
          2 => 
            array (size=4)
              0 => string '10view2' (length=7)
              1 => string '10ins2' (length=6)
              2 => string '10edit2' (length=7)
              3 => string '10del2' (length=6)
          3 => 
            array (size=4)
              0 => string '10view3' (length=7)
              1 => string '10ins3' (length=6)
              2 => string '10edit3' (length=7)
              3 => string '10del3' (length=6)
      11 => 
        array (size=7)
          1 => 
            array (size=4)
              0 => string '11view1' (length=7)
              1 => string '11ins1' (length=6)
              2 => string '11edit1' (length=7)
              3 => string '11del1' (length=6)
          2 => 
            array (size=4)
              0 => string '11view2' (length=7)
              1 => string '11ins2' (length=6)
              2 => string '11edit2' (length=7)
              3 => string '11del2' (length=6)
          3 => 
            array (size=4)
              0 => string '11view3' (length=7)
              1 => string '11ins3' (length=6)
              2 => string '11edit3' (length=7)
              3 => string '11del3' (length=6)

我想将它转换为数组:

1 => array(10 => array( 
              0 => string '10view1' (length=7)
              1 => string '10ins1' (length=6)
              2 => string '10edit1' (length=7)
              3 => string '10del1' (length=6)),
           11 => array(
              0 => string '11view1' (length=7)
              1 => string '11ins1' (length=6)
              2 => string '11edit1' (length=7)
              3 => string '11del1' (length=6) 
           )
        ),
2 => array(
          10 => array(
             0 => string '10view2' (length=7)
              1 => string '10ins2' (length=6)
              2 => string '10edit2' (length=7)
              3 => string '10del2' (length=6)
          ),
          11 => array(
               0 => string '11view2' (length=7)
              1 => string '11ins2' (length=6)
              2 => string '11edit2' (length=7)
              3 => string '11del2' (length=6)
          )
      ),
3 => array(
        10 => array(
             0 => string '11view3' (length=7)
              1 => string '11ins3' (length=6)
              2 => string '11edit3' (length=7)
              3 => string '11del3' (length=6)
          ),
          11 => array(
             0 => string '11view1' (length=7)
              1 => string '11ins1' (length=6)
              2 => string '11edit1' (length=7)
              3 => string '11del1' (length=6) 
          )
    )

请提前帮助我解决这个问题。

对我来说很明显如何使用foreach循环实现它,但我想知道是否可以使用像array_map或array_walk这样的PHP数组函数来实现这一点。请使用PHP 5.3作为回调函数。谢谢!

1 个答案:

答案 0 :(得分:0)

使用数组映射可以实现这样的

<?php

function merge_array($n, $m)
{
   $data = array();

   $data[10] = $n;
   $data[11] = $m;

   return $data;

}

$c = array_map("merge_array", $array[10], $array[11]);
print_r($c);

使用for循环很简单,你可以像这样实现

$count_10 = count($array[10]);

$count_11 = count($array[11]);

$count = ($count_10>$count_11)?$count_10:$count_11;

$data = array();

for($i=0; $i<$count; $i++)
{

  if(isset($array[10][$i]))
  {
       $data[$i][] =  $array[10][$i];
  }

  if(isset($array[11][$i]))
  {
       $data[$i][] =  $array[11][$i];
  }

}