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?;
}
dan
  • 55
  • 8

2 Answers2

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

vee
  • 38,255
  • 7
  • 74
  • 78
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
}
Community
  • 1
  • 1
adrian.budau
  • 349
  • 1
  • 6