如何使用多维数组的array_map/array_walk函数



我有以下多维数组:

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循环实现这一点,但我想知道是否有可能使用PHP数组函数如array_map或array_walk来实现这一点。回调函数请使用PHP 5.3。谢谢你!

使用数组映射可以实现如下

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

相关内容

  • 没有找到相关文章

最新更新