1

Given 2 associative arrays:

$fruits  = array ( "d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple" );
$fruits2 = array ( "e" => "lemon", "f" => "apple",  "g" => "melon",  "h" => "apple" );

I would like to do something like:

for ( $n = count($fruits), $i = 0; $i < $n; $i++)
{
  $test = (bool) $fruits[$i] == $fruits2[$i];
}

This can not work as I am using associative array. What would be the best way to go to achieve that? (This loops is going to be ran intensity so I would like to keep it as light as possible)

EDIT to give more detail on what I am trying to do:

Here is a better example of what I am trying to achieve:

$array   = array ( 1,2,3,4,3,2 );
$array2  = array ( 9,6,3,4,3,2 );
$counts  = array_count_values( $words );
$counts2 = array_count_values( $words2 );

Given the arrays above I need to calculate which array as the highest duplicate integers. Imagine a poker game, comparing two hands that each contain duplicate cards, how to evaluate which set of duplicate (whether double, triple or quadruple ) as the highest value.

hakre
  • 193,403
  • 52
  • 435
  • 836
silkAdmin
  • 4,640
  • 10
  • 52
  • 83

4 Answers4

3

Use array array_values ( array $input ) function and compare them.

 $value1=array_values($fruits);
 $value2=array_values($fruits2);

 for ( $i = 0; $i < count($value1); $i++)
 {
   $test[] = $value1[$i] == $value2[$i] ? TRUE : FLASE;
 }
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • Thanks AVD, though i realize now that my question wasn't correctly formatted as i also need to manipulate the keys at the same time, but i guess i could use the same process with array_keys as well. – silkAdmin Dec 06 '11 at 06:07
2

Got it working this way :

$n = count($fruits);
for ( $i = 0; $i < $n; $i++)
{
  $cur_vals_1 = each ($fruits);
  $cur_vals_2 = each ($fruits2);

  $sum1 += $cur_vals_1['key'] * $cur_vals_1['value'];

  ...
}
hakre
  • 193,403
  • 52
  • 435
  • 836
silkAdmin
  • 4,640
  • 10
  • 52
  • 83
1

You're probably chasing the wrong solution. To find the highest duplicate in an array I'd use this (PHP 5.3+ syntax):

max(array_keys(array_filter(array_count_values($array), function ($i) { return $i >= 2; })))

Do this for both arrays and compare which result is higher. Trying to compare both against each other at the same time is too convoluted.

deceze
  • 510,633
  • 85
  • 743
  • 889
-2

You don't need the ternary operator.
Also, be careful with count() as it will fail if your array is null.
They also need to be equal lengths or you'll get an error.

if( is_array($value1)  && is_array($value2) && count($value1)==count($value2) ) {
    for ( $i = 0; $i < count($value1); $i++)
    {
        $test[] = ($value1[$i] == $value2[$i]);
    }
}
Joseph Lust
  • 19,340
  • 7
  • 85
  • 83
  • ouch, -2. Thanks for the slap! Can you proffer why we shouldn't do null checks or confirm that the code will not fail before running it? Thanks. – Joseph Lust Dec 06 '11 at 16:03
  • Because the question says that those are two arrays of the same length. However your code doesn't work with the non-numeric keys here so checked something not to be checked and just didn't work. – hakre May 03 '15 at 07:17