Jump to content

Counting values in multidimensional arrays


Samuz

Recommended Posts

I have an array with something along the lines of..

 

Array
(
    [0] => Array
    (
        [name] => Michael
    )
    
    [1] => Array
    (
        [name] => Michael
    )
    
    [2] => Array
    (
        [name] => Jordan
    )
    
    [3] => Array
    (
        [name] => Marie
    )
    
    [4] => Array
    (
        [name] => Marie
    )
    
    [5] => Array
    (
        [name] => Michael
    )
    
)

 

My final result should produce an array something like..

 

Array
(
    [0] => Array
    (
        [Michael] => 3
    )
    
    [1] => Array
    (
        [Jordan] => 1
    )
    
    [2] => Array
    (
        [Marie] => 2
    )
)

Or something like that.

 

I have a pretty good idea that'll i'd need to use foreach twice to get 2 layers 'deep' but i'm just stuck from there.

 

 

array_count_values()

 

How is the original array created? There might be a way to get it in a more useful format to start with.

Thanks for your reply.

 

It's a direct pull from my database. So the array comes in the format of my structure (index = field name, values = field values)

 

array_count_values()

 

I saw this function but I don't see how that will work for a multidimensional array? Unless if possibly I can just merge everything into a single array while keeping their original indexes & values?

It's a direct pull from my database. So the array comes in the format of my structure (index = field name, values = field values)

Really?! There are two problems with that statement. 1) You could EASILY put the values into a single dimensional array when processing the DB results. 2) If you need to know the count of unique names - then just do that using your DB query! Something such as:

SELECT name, COUNT(name) as count
FROM table
GROUP BY name

 

I actually have a very simple solution for getting the result you asked for using the multi-dimensional array, but that is not the solution that you should be using.

It's a direct pull from my database. So the array comes in the format of my structure (index = field name, values = field values)

Really?! There are two problems with that statement. 1) You could EASILY put the values into a single dimensional array when processing the DB results. 2) If you need to know the count of unique names - then just do that using your DB query! Something such as:

SELECT name, COUNT(name) as count
FROM table
GROUP BY name

 

I actually have a very simple solution for getting the result you asked for using the multi-dimensional array, but that is not the solution that you should be using.

Thanks mate. Someone needs to slap me for not looking for easier approaches first. heh

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.