0

I can't seem to get any results from the following foreach code. I can't seem to recursively run through the array and i can't make a smaller array of this array. I just want to cherry pick data from the array and place the data in much smaller arrays.

PHP CODE

$ii = 0;
foreach ($array as $key => $value)
      { 
       $vv = $value;
       if($vv== 'Alpha')
             {
         $ii++;
          $list[$ii]['info'] = $vv;   // my new array
         }  
      }  

PHP print_r($array)

Array
(
[0] => Array
    (
        [moredetails] => Array
            (
                [moredetails] => Array
                    (
                        [0] => Array
                            (
                                [productId] => 9999
                                [productName] => Name
                                [productType] => Alpha 
                                [serialNumber] => 
                                [serviceItemId] => 123456                                   
                            )

                      [1] => Array
                            (


                                [productId] => 8888
                                [productName] => Name
                                [productType] => Beta
                                [serialNumber] => 
                                [serviceItemId] => 123456                                   
                            )
                    )
             )
      )
)    

Ideal Result

Array
(
  [0]
    (
     [productID] => 9999
     [productName] => Name
     {productType] => Alpha
     )
 )

Solution Found!

function miniArray($array, &$simple, $ii)
{
   foreach($array as $key => $value)
   {

       if(is_array($value)){            
        $ii++;
            miniArray($value, $simple, $ii);
       }
       else
       {
        $simple[$ii][$key] = $value;

    }   

   }
}

miniArray($service, $simple_array, 0);
5
  • 1
    foreach doesn't loop recursively through an array. you'll have to write a custom function for that. Commented Nov 5, 2012 at 18:28
  • That makes sense why i wasn't getting any results. There is no built in PHP function that loops through a multi-dim array? Commented Nov 5, 2012 at 18:30
  • Nope not really, but if you check out the PHP manual on pages like array_key_exists, array_search etc. there are a multitude of recursive array search functions to pick from. Commented Nov 5, 2012 at 18:32
  • Sure the is. One is called array_walk_recursive. I suggest you look around in the PHP manual a little to find even more options: php.net/arrays and for the basics of the array itself: php.net/array Commented Nov 5, 2012 at 18:35
  • Quick note: If you plan on rolling out your own looping mechanism, remember an array may reference itself (or in a child array), effectively causing infinite recursion. Commented Nov 5, 2012 at 18:38

2 Answers 2

1
<?php
    function miniArray($array, &$simple)
    {
        foreach($array as $key => $value)
        {
            if(is_array($value))
                miniArray($value, $simple);
            else
                $simple[] = $value;
        }
    }

    miniArray($complex_array, $simple_array);
?>
Sign up to request clarification or add additional context in comments.

4 Comments

I edited my question but your answer was what i used to create my function! Thanks a Ton!
You don't have to pass $ii as a parameter. $simple[] = $value; sintax will add $value to the next available index in $simple. e.g.: The first time it executes, it'll be interpreted as $simple[0] = $value;. The second time will be $simple[1] = $value; and so on.
Unless you're storing an array in the index that is the depth they were found. If so, ignore this.
I am storing an array index. So thats why. :)
1

foreach will only go through the outer layer of the array. On first loop, $key will be 0 and $value will be an array - which will never match 'Alpha'.

What you'll want is a recursive function. PHP provides us a nice shortcut, array_walk_recursive, which will allow you to provide your own function that will get applied for each element recursively.

$list = array(); //your new array

function walker($value, $key) {
    if ($value == 'Alpha') $list[] = $value;
}

array_walk_recursive($array, 'walker'); //$array is your original array

2 Comments

This looks good. How would i go about adding the productid, productname to the $list[] if $value == 'Alpha'?
Hmmm, if that's what you need, then on a closer look, array_walk_recursive is not what you need after all. Sorry, my bad. According to the manual, "Any key that holds an array will not be passed to the function", so it doesn't seem you'll have any way of linking the right values together. Best to go with a custom recursion then, more along the lines of @freon's.

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.