Brutnus
Brutnus

Reputation: 163

search php array for specific values using foreach

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);

Upvotes: 0

Views: 5240

Answers (2)

Rick
Rick

Reputation: 1258

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

Upvotes: 1

Matheus Azevedo
Matheus Azevedo

Reputation: 898

<?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);
?>

Upvotes: 1

Related Questions