JSHINER Posted February 25, 2009 Share Posted February 25, 2009 Array ( [0] => Array ( [0] => apple [1] => ball [2] => tiger ) [1] => Array ( [0] => ball [1] => square [2] => tiger [3] => apple ) [2] => Array ( [0] => ball [1] => tire [2] => square ) ) From that I want to create an array that counts how many of each word exists. To display as this: array('apple' => 2, 'ball' => 3, 'tiger' => 2, 'square' => 2, 'tire' => 1) How can I do this? Quote Link to comment https://forums.phpfreaks.com/topic/146880-solved-counting-values-in-an-array/ Share on other sites More sharing options...
Mark Baker Posted February 25, 2009 Share Posted February 25, 2009 $testArray = array( array( 'apple', 'ball', 'tiger'), array( 'ball', 'square', 'tiger', 'apple'), array( 'ball', 'tire', 'square'), 'square' ); function flattenArray($array) { if(!is_array ( $array ) ){ $array = array ( $array ); } $arrayValues = array(); foreach ($array as $value) { if (is_scalar($value)) { $arrayValues[] = $value; } elseif (is_array($value)) { $arrayValues = array_merge($arrayValues, flattenArray($value)); } else { $arrayValues[] = $value; } } return $arrayValues; } $wordCounts = array_count_values(flattenArray($testArray)); print_r($wordCounts); Quote Link to comment https://forums.phpfreaks.com/topic/146880-solved-counting-values-in-an-array/#findComment-771159 Share on other sites More sharing options...
JSHINER Posted February 25, 2009 Author Share Posted February 25, 2009 Great, thanks! One question... some of the values have a blank white space infront of them (issue on my end) - so instead of "ball" it's " ball" - which messes up the counting. How can I get rid of that whitespace? Quote Link to comment https://forums.phpfreaks.com/topic/146880-solved-counting-values-in-an-array/#findComment-771167 Share on other sites More sharing options...
allworknoplay Posted February 25, 2009 Share Posted February 25, 2009 You could try using ltrim. $testArray = array( array( 'apple', 'ball', 'tiger'), array( 'ball', 'square', 'tiger', 'apple'), array( 'ball', 'tire', 'square'), 'square' ); function flattenArray($array) { if(!is_array ( $array ) ){ $array = array ( $array ); } $arrayValues = array(); foreach ($array as $value) { if (is_scalar($value)) { $arrayValues[] = ltrim($value); } elseif (is_array($value)) { $arrayValues = array_merge($arrayValues, flattenArray(ltrim($value))); } else { $arrayValues[] = ltrim($value); } } return $arrayValues; } $wordCounts = array_count_values(flattenArray($testArray)); print_r($wordCounts); Quote Link to comment https://forums.phpfreaks.com/topic/146880-solved-counting-values-in-an-array/#findComment-771170 Share on other sites More sharing options...
JSHINER Posted February 25, 2009 Author Share Posted February 25, 2009 allworknoplay - for some reason that code you put just returns a single number? Quote Link to comment https://forums.phpfreaks.com/topic/146880-solved-counting-values-in-an-array/#findComment-771179 Share on other sites More sharing options...
JSHINER Posted February 25, 2009 Author Share Posted February 25, 2009 Got it! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/146880-solved-counting-values-in-an-array/#findComment-771183 Share on other sites More sharing options...
JSHINER Posted February 25, 2009 Author Share Posted February 25, 2009 Ok another quick question. That outputs: Array ( [apple] => 2 [ball] => 3 [tiger] => 2 [square] => 2 [tire] => 1 ) How can I get it to go into an array: $terms[] = array('term' => $term, 'counter' => $counter); Where obviously 'term' is ball, square, etc and 'counter' is the # for that? Quote Link to comment https://forums.phpfreaks.com/topic/146880-solved-counting-values-in-an-array/#findComment-771192 Share on other sites More sharing options...
Mark Baker Posted February 26, 2009 Share Posted February 26, 2009 $newWordCounts = array(); foreach($wordCounts as $word => $count) { $newWordCounts[] = array('term' => $word, 'counter' => $count ); } print_r($newWordCounts); Quote Link to comment https://forums.phpfreaks.com/topic/146880-solved-counting-values-in-an-array/#findComment-771681 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.