geckoo Posted November 7, 2011 Share Posted November 7, 2011 I have the following array: Array ( [item0] => Array ( [0] => name1 [1] => name2 [2] => name3 [3] => name4 [4] => name5 ) etc... Is it possible to count the number of times a name exists in the compleet array? Thanx in advance! Quote Link to comment https://forums.phpfreaks.com/topic/250608-count-array/ Share on other sites More sharing options...
Andy-H Posted November 7, 2011 Share Posted November 7, 2011 count $arr = range(1,9); echo count($arr); // 9 Quote Link to comment https://forums.phpfreaks.com/topic/250608-count-array/#findComment-1285804 Share on other sites More sharing options...
geckoo Posted November 7, 2011 Author Share Posted November 7, 2011 Thanx for the fast reply but i was maybe the following example is better: Array ( [item0] => Array ( [0] => peter [1] => michael [2] => kirsten [3] => henry [4] => james ) [item1] => Array ( [0] => henry [1] => james [2] => peter [3] => brad [4] => ford ) So what I need to know is how many times for instance name "james" is represented in the entire array. james = 2 peter = 2 henry = 1 etc... Quote Link to comment https://forums.phpfreaks.com/topic/250608-count-array/#findComment-1285806 Share on other sites More sharing options...
Adam Posted November 7, 2011 Share Posted November 7, 2011 array_count_values - did you even attempt to find this yourself? It was the second result on Google for "php count array". Quote Link to comment https://forums.phpfreaks.com/topic/250608-count-array/#findComment-1285808 Share on other sites More sharing options...
geckoo Posted November 7, 2011 Author Share Posted November 7, 2011 I did actually find it and try it, but couldn't get it to work. I do search before I ask. Sorry to upset you... I think i'll have to take a look at the array. Quote Link to comment https://forums.phpfreaks.com/topic/250608-count-array/#findComment-1285810 Share on other sites More sharing options...
haku Posted November 7, 2011 Share Posted November 7, 2011 I don't think array_count_values() will work, as it's a multi-dimensional array. You could maybe do this: $total_count = array(); foreach($array as $values) { $new_count = array_count_values($values); foreach($new_count as $key=>$count) { if(isset($total_count[$key]) { $total_count[$key] += $count; } else { $total_count[$key] = $count; } } } $total_count will be an array of names with the count for each name. Note: untested. may also not need the if/else statement (test it to find out). This may work: $total_count = array(); foreach($array as $values) { $new_count = array_count_values($values); foreach($new_count as $key=>$count) { $total_count[$key] += $count; } } I'm just not sure if the += sign works if the original array key isn't set. Maybe try the second block of code before the first. Quote Link to comment https://forums.phpfreaks.com/topic/250608-count-array/#findComment-1285828 Share on other sites More sharing options...
Adam Posted November 7, 2011 Share Posted November 7, 2011 I don't think array_count_values() will work, as it's a multi-dimensional array. Then I apologise for my remarks, geckoo Quote Link to comment https://forums.phpfreaks.com/topic/250608-count-array/#findComment-1285834 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.