A multidimensional array stores each list array inside another array

I have nested multidimensional arrays that can be 2 or 3 levels deep. Inside, I may or may not have a list. I need to iterate over an 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 save 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
  )

);
+4
source share
2 answers

You can use array_walk_recursive () to get the key 1,2 and are not an element of the array, for example, here is a 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);

Change for uncommitted length and unordered index, live demo.

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

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

Source: https://habr.com/ru/post/1679212/


All Articles