Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting Items Inside Multidimensional Associative Array

So I have the following array of data in PHP

$array = array(
           "animal" => "panda",
           "location" => "San Diego",
           "age" => "2",            
         ),
         array(
           "animal" => "tiger",
           "location" => "Bronx",
           "age" => "5",            
         ),
         array(
           "animal" => "panda",
           "location" => "Bronx",
           "age" => "3",            
         ),
         array(
           "animal" => "tiger",
           "location" => "bronx",
           "age" => "3",            
         ),
         array(
           "animal" => "panda",
           "location" => "San Diego",
           "age" => "2",            
         )
)

What I want to do is convert this into an associative array that will contain the count for the animals, locations and age. so if I wanted to see how many pandas are in san diego and are age 2 I would access the new array with $newArray['panda']['San Diego']['2'] and the output would be 2.

My issue is I can easily run a loop and build an array and count the items when it is completely static like so.

foreach($array as $a) {

   $newArray[$a['animal']][$a['location']][$a['age']] += 1;

}

However I want to know how to accomplish the same concept when the number of keys is dynamic. for instance what if there are only location and animal in one call and sometimes there might be location, animal, age, and gender in another call.

There are some examples I have found that expound on doing this for associative arrays but not for multidimensional arrays with this particular use case.

Any Thoughts?

like image 936
wmfrancia Avatar asked Mar 12 '23 09:03

wmfrancia


1 Answers

The Idea

It looks to me as if you're trying to do too much in one go. Instead of transforming your array to contain all your answers, consider making a function to run queries against it.

The Solution

/**
 * @param array $animals The whole animals array
 * @param array $query Array containing keys and values you want to filter by
 *
 * @return array Animals that match $query
 */
function filterAnimals($animals, $query) {

    return array_filter($animals, function ($animal) use ($query) {
        // Check if $animal has every key and the corresponding value from $query
        foreach ($query as $key => $value) {
            if (!isset($animal[$key]) || $animal[$key] != $value) {
                return false;
            }
        }

        return true;
    });
}

filterAnimals() filters your $animals array and returns only the animals that have the common keys and values with the $query array. Searching for all pandas from San Diego would look like this:

$result = filterAnimals($animals, [
    'animal' => 'panda',
    'location' => 'San Diego'
]);

Then, if you wish, you can count the $result:

$count = count($result);
like image 181
Kuba Birecki Avatar answered Mar 16 '23 04:03

Kuba Birecki