Reputation: 55
I'm using an array_push
to add to an array, that of which how many times a card is there.
$array = array();
foreach($aiarray as $card) {
$cardss = card($card);
array_push($array, $cardss);
}
asort($array);
print_r($array);
This gives me what I need, but now how do I search the array and for say;
pseudo
if (*array contains > 2 of the same word) {
find out what word;
does the array contain more than one?;
}
Upvotes: 0
Views: 217
Reputation: 349
Array Count Values and then PHP Get Highest Value from Array
Just do
$count = array_count_values($array);
$max_times = max($count);
if ($max_times > 2) {
$word = array_search($max_times, $count);
.... whatever you need
}
Upvotes: 1
Reputation: 38645
How about this?
if (in_array($word, $array)) {
$array_keys = array_flip($array);
$number_of_words = count(array_keys($array_keys, $word));
if ($number_of_words > 1) {
echo "array contains word more than once";
}
}
Functions used: in_array, array_flip, and array_keys
Upvotes: 1