oliverj777 Posted April 5, 2012 Share Posted April 5, 2012 Hello, I have an array, and I want to search the array for a particular 'word' and return how many times that 'word' is in the array. I tried using the search_array, but that just seems to find the first 'word' and return it. What I want it this: $array = array(0-blue, 1-yellow, 2-red, 3-blue, 4-blue, 5-blue, 6-yellow); Lets say I want to see how many times the word 'blue' is in the array: $counter = 4; How would I achieve this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/260407-php-search-array-for-same-values/ Share on other sites More sharing options...
AyKay47 Posted April 5, 2012 Share Posted April 5, 2012 http://www.php.net/manual/en/function.array-count-values.php Quote Link to comment https://forums.phpfreaks.com/topic/260407-php-search-array-for-same-values/#findComment-1334658 Share on other sites More sharing options...
oliverj777 Posted April 5, 2012 Author Share Posted April 5, 2012 http://www.php.net/manual/en/function.array-count-values.php This doesn't let me search for a specific word/value Quote Link to comment https://forums.phpfreaks.com/topic/260407-php-search-array-for-same-values/#findComment-1334664 Share on other sites More sharing options...
Drummin Posted April 5, 2012 Share Posted April 5, 2012 Part of the issue "might" be the way you've made your array. Here's two examples. $search="blue"; $array = array("blue", "yellow", "red", "blue", "blue", "blue", "yellow"); $counts= array_keys($array,$search); $counter=count($counts); echo " $counter"; echo "<br/>"; $counts2=array_count_values($array); $counter2=$counts2[$search]; echo "$counter2"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/260407-php-search-array-for-same-values/#findComment-1334667 Share on other sites More sharing options...
oliverj777 Posted April 5, 2012 Author Share Posted April 5, 2012 Part of the issue "might" be the way you've made your array. Here's two examples. $search="blue"; $array = array("blue", "yellow", "red", "blue", "blue", "blue", "yellow"); $counts= array_keys($array,$search); $counter=count($counts); echo " $counter"; echo "<br/>"; $counts2=array_count_values($array); $counter2=$counts2[$search]; echo "$counter2"; ?> Thanks, your first option works like a charm. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/260407-php-search-array-for-same-values/#findComment-1334670 Share on other sites More sharing options...
AyKay47 Posted April 5, 2012 Share Posted April 5, 2012 http://www.php.net/manual/en/function.array-count-values.php This doesn't let me search for a specific word/value sure it does: $array = array('blue', 'yellow', 'red', 'blue', 'blue', blue', yellow'); $count_arr = array_count_values($array); $blue_count = $count_arr['blue']; //prints 4 Quote Link to comment https://forums.phpfreaks.com/topic/260407-php-search-array-for-same-values/#findComment-1334688 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.