Samuz Posted August 7, 2012 Share Posted August 7, 2012 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 https://forums.phpfreaks.com/topic/266752-counting-values-in-multidimensional-arrays/ Share on other sites More sharing options...
Psycho Posted August 7, 2012 Share Posted August 7, 2012 array_count_values() will get you started. How is the original array created? There might be a way to get it in a more useful format to start with. Link to comment https://forums.phpfreaks.com/topic/266752-counting-values-in-multidimensional-arrays/#findComment-1367394 Share on other sites More sharing options...
Samuz Posted August 7, 2012 Author Share Posted August 7, 2012 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 https://forums.phpfreaks.com/topic/266752-counting-values-in-multidimensional-arrays/#findComment-1367395 Share on other sites More sharing options...
Psycho Posted August 7, 2012 Share Posted August 7, 2012 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 https://forums.phpfreaks.com/topic/266752-counting-values-in-multidimensional-arrays/#findComment-1367397 Share on other sites More sharing options...
Samuz Posted August 7, 2012 Author Share Posted August 7, 2012 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 https://forums.phpfreaks.com/topic/266752-counting-values-in-multidimensional-arrays/#findComment-1367398 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.