0

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?;
}

2 Answers 2

1

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

0
1

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
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.