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
  )

);
5
  • 2
    We are not here to solve your homework!!! What have you tried? Commented Jun 14, 2017 at 6:52
  • i wish it was my homework i have try foreach and isset and i cant get it to work Commented Jun 14, 2017 at 6:56
  • try recursion too Commented Jun 14, 2017 at 6:59
  • It appears this data is being pulled from some sort of database. Perhaps a better solution would be to normalize the data (make the data all formatted the same) Commented Jun 14, 2017 at 7:01
  • i cant its a json that come from anther source Commented Jun 14, 2017 at 7:20

2 Answers 2

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;
        }
    }
}
Sign up to request clarification or add additional context in comments.

10 Comments

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 array_work_recursive() is recursive funciton. Also works for your 10 situation.
but i may have the nasted array in 4 postion or 8 not only 1-2
if length is fixed, for example (1-n), just a small modification will do.
@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)
|
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;
}

3 Comments

@shlomicohen you have to just replace pseudo code with some functions
how can i run it?
hey @shlomicohen, just write the code with this logic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.