多维数组将每个列表数组存储在另一个数组中 [英] multidimensional array store each list array inside another array

查看:106
本文介绍了多维数组将每个列表数组存储在另一个数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有嵌套的多维数组,其深度可能为2或3级.在其中,我可能有也可能没有列表错误.我需要遍历数组:

I have nested multidimensional arrays that may be 2 or 3 levels deep. Inside it I may or may not have list 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
  )

);

推荐答案

您可以使用array_walk_recursive()获取密钥1,2,而不是数组元素,像这样,请检查

You can use array_walk_recursive() to get the key 1,2 and are not array element, like this, check the live demo here.

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

使用第三个参数为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;
        }
    }
}

这篇关于多维数组将每个列表数组存储在另一个数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆