4

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
  )

);
LF00
  • 27,015
  • 29
  • 156
  • 295
shlomicohen
  • 101
  • 1
  • 7

2 Answers2

4

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.

Extend the array_walk_recursive() with a third parameter for the callfunction to indicate if it's at the start of an subarray.

$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;
        }
    }
}
LF00
  • 27,015
  • 29
  • 156
  • 295
  • thankk but i can have a 10 arrays inside a array not only 3 and its a random order i may have array on 4 postion and in another array its can be on the 8 postion – shlomicohen Jun 14 '17 at 07:26
  • @shlomicohen `array_work_recursive()` is recursive funciton. Also works for your 10 situation. – LF00 Jun 14 '17 at 07:28
  • but i may have the nasted array in 4 postion or 8 not only 1-2 – shlomicohen Jun 14 '17 at 07:30
  • if length is fixed, for example `(1-n)`, just a small modification will do. – LF00 Jun 14 '17 at 07:35
  • not fixed i have 60 arrays inside the main array and each is anther nustetd array with random length – shlomicohen Jun 14 '17 at 07:38
  • @shlomicohen you can check it now – LF00 Jun 14 '17 at 07:53
  • thaks! but its only working when the array with array inside is a 1 postion it can be on any postion – shlomicohen Jun 14 '17 at 08:12
  • @shlomicohen you can check my answer now. – LF00 Jun 14 '17 at 09:34
  • 1
    @KrisRoofe You are genius man!! I learned something new for me in php. Because the logic you used was beyond my knowledge, now I have one more logic in collection.;) It will help me in future!. Thanks (y) – Parag Soni Jun 14 '17 at 09:45
  • @shlomicohen you need to do your homework, if you don't know how to use my code. Just pay some efffort, you will run it. Sorry, I cannot any more for you. – LF00 Jun 14 '17 at 09:57
0

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;
}
Shubham Agrawal
  • 559
  • 6
  • 15