简体   繁体   English

多维数组将每个列表数组存储在另一个数组中

[英]multidimensional array store each list array inside another array

I have nested multidimensional arrays that may be 2 or 3 levels deep. 我有嵌套的多维数组,可能是2或3级深。 Inside it I may or may not have list arrray. 在里面我可能有也可能没有列表arrray。 I need to loop over the array: 我需要遍历数组:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => cat_name_1
            [list] => Array
                (
                    [1] => swgdgbdg
                    [2] => xcbcxb
                )

        )

    [1] => Array
        (
            [id] => 3
            [name] => cat_name_3
            [list] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [name] => cat_name_1
                            [list] => Array
                                (
                                    [1] => 543h54h
                                    [2] => 54hrhhfr2
                                )

                        )

                    [1] => Array
                        (
                            [id] => 2
                            [name] => cat_name_2
                            [list] => Array
                                (
                                    [1] => eherfherh
                                    [2] => 4564642
                                )

                        )

                    [2] => Array
                        (
                            [1] => erggb45yt
                            [2] => jyuk768k
                        )

                    [3] => Array
                        (
                            [1] => sdgsdgsdg
                            [2] => 4tg34g34g
                        )

                )

        )

and store the following in another array: 并将以下内容存储在另一个数组中:

        array (
  0 => array ( 
      [1] => swgdgbdg
      [2] => xcbcxb
  ) ,
  1 => array(
      [1] => 543h54h
      [2] => 54hrhhfr2
  ) ,
  2 => array(
      [1] => eherfherh
      [2] => 4564642
  ),
  3 => array(
     [1] => erggb45yt
     [2] => jyuk768k
  ),
  4 => array(
     [1] => sdgsdgsdg
     [2] => 4tg34g34g
  )

);

You can use array_walk_recursive() to get the key 1,2 and are not array element, like this, check the live demo here . 您可以使用array_walk_recursive()来获取密钥1,2而不是数组元素,如下所示,请在此处查看实时演示

$result = [];
$temp = [];
array_walk_recursive($array, function($v, $k) use(&$result, &$temp){
if($k == 1)
  $temp[$k] = $v;
if($k == 2)
{
  $temp[$k] = $v;
  $result[] = $temp;
}
});
print_r($result);

Edit for unfixed length and unordered index, live demo. 编辑不固定长度和无序索引, 现场演示。

Extend the array_walk_recursive() with a third parameter for the callfunction to indicate if it's at the start of an subarray. 使用callfunction的第三个参数扩展array_walk_recursive(),以指示它是否在子数组的开头。

$result = [];
$temp = [];
$length = 0;
uarray_walk_recursive($array, function($v, $k, $f) use(&$result, &$temp, &$length){
if(is_numeric($k)) {
  if($f && $length != 0) {
    $result[] = $temp;
    $length = 0;
    $temp = [];
  }
  $temp[$k] = $v;
  $length++;
}
});
$result[] = $temp;
print_r($result);


function uarray_walk_recursive(&$input, $funcname)
{
    if (!is_callable($funcname)) {
        if (is_array($funcname)) {
            $funcname = $funcname[0] . '::' . $funcname[1];
        }
        user_error('uarray_walk_recursive() Not a valid callback ' . $user_func,
            E_USER_WARNING);
        return;
    }

    if (!is_array($input)) {
        user_error('uarray_walk_recursive() The argument should be an array',
            E_USER_WARNING);
        return;
    }

    $args = func_get_args();
    $flag = true;

    foreach ($input as $key => $item) {
        if (is_array($item)) {
            $args[2] = false;
            $flag = true;
            uarray_walk_recursive($item, $funcname, $args);
            $input[$key] = $item;
        } else {

            $args[2] = $flag;
            $flag = false;
            $args[0] = &$item;
            $args[1] = &$key;
            call_user_func_array($funcname, $args);
            $input[$key] = $item;
        }
    }
}

PseudoCode 伪代码

function getSomething(var arr)
{
   flag = 0;
   output = []
    for( i = 0 to arr.length)
    {
      check arr[i] is array, 
      if yes,then  flag = 1 and output.add(getSomething(arr[i]))
      if not, continue 
    }
  if flag =0,then return arr
  else return output;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM