PHP: Find Keys / Values in a Multidimensional Array. Help!

Associate
Joined
9 Nov 2005
Posts
767
Location
places..
Hello,

<noob>

Hi I'm using an API that returns JSON. I have converted the return JSON using "json_decode", into a multidimensional array.

I need to find the number of a certain key with a specified Value (in this case bool = true). I don't seem to be able to find any functions to do this.

In a nutshell each of the child arrays is a re-iteration of the same array, I just need to find how many (child arrays) have a certain key that has a true value, I.E. how many of the child arrays have a true value for the 'foo' key.

I'm sure there is probably a function to do this, or will I have to foreach though and add it up? It's a little frustrating!

Thanks for any help

</noob>
 
er, what?

I got as far as you need to traverse a multi-dimensional array, and that you are looking for true within those arrays and want the corresponding key, but the thing about the "child arrays is a re-iteration of the same array" has me raising an eyebrow,as does the bit about "adding up".
 
Sorry jestar, I know it's not the best explanation. Well it's carp.

Perhaps a little more info would help. I simply need the number of occurrences of a specified key, "foo" being true. The key will be in each of the sub-arrays so to speak.

Each of the arrays contains certain attributes, for a list of objects. Say for example, the array describes a car park, with a sub array for each car, containing information such as make, model, colour date of manufacture etc.

I want to go though and find the number of times there is say, a "red" car. In this case the key is boolean, and I looking for the number of times the key is true.

Is this a better description? I'm no good at coming the lingo :P

Cheers



EDIT: looks like this is what i was after, I've not yet fully read it, but it looks like it foreach'es though the array, which is what i meant by adding up :P
 
Last edited:
Code:
$car_parks = Array (
    'car_park_1' => Array (
            '1' => Array ('color' => red),
            '2' => Array ('color' => red)
        ),
    'car_park_2' => Array (
            'car_1' => Array ('color' => red),
            'car_2' => Array ('color' => blue)
        )
);

Like that? Except red == true

I think maybe something like

Code:
$i = 1;
$num = 0;
foreach ($car_parks as  $park_name) {
    foreach ($park_name as $car => $value) {
         if($value[color] == 'red') {
         $num += $i;
         }
    }
}

echo count($num);


I'm pretty sure this is a crap noob way of doing it.
 
Last edited:
something like this, you'll need to debug it yourself.

PHP:
$bar = array();

function foo($arr = null) {
    global $bar;
    foreach($arr as $key=>$val) {
        if (is_array($val)) {
            foo($val);
        } else {
            if (isset($bar[$key])) {
                $bar[$key] = $bar[$key]++;
            } else {
                $bar[$key]=0;
            }
        }
    }
}
 
Back
Top Bottom