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.

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.