计算两个关联数组的最高重复整数

时间:2021-09-13 11:41:29

Given 2 associative arrays:

给定2个关联数组:

$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.

鉴于上面的数组,我需要计算哪个数组是最高的重复整数。想象一下扑克游戏,比较两只手,每只手都包含重复的卡片,如何评估哪一组重复(无论是双倍,三倍还是四倍)作为最高值。

4 个解决方案

#1


3  

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

使用array array_values(array $ input)函数并比较它们。

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

 for ( $i = 0; $i < count($value1); $i++)
 {
   $test[] = $value1[$i] == $value2[$i] ? TRUE : FLASE;
 }

#2


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'];

  ...
}

#3


1  

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

你可能正在追逐错误的解决方案。要在数组中找到最高重复项,我会使用它(PHP 5.3+语法):

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.

对两个数组执行此操作并比较哪个结果更高。试图同时将两者相互比较是太复杂了。

#4


-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.

您不需要三元运算符。另外,请注意count(),因为如果数组为null,它将失败。它们也需要相同的长度,否则你会收到错误。

if( is_array($value1)  && is_array($value2) && count($value1)==count($value2) ) {
    for ( $i = 0; $i < count($value1); $i++)
    {
        $test[] = ($value1[$i] == $value2[$i]);
    }
}

#1


3  

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

使用array array_values(array $ input)函数并比较它们。

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

 for ( $i = 0; $i < count($value1); $i++)
 {
   $test[] = $value1[$i] == $value2[$i] ? TRUE : FLASE;
 }

#2


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'];

  ...
}

#3


1  

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

你可能正在追逐错误的解决方案。要在数组中找到最高重复项,我会使用它(PHP 5.3+语法):

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.

对两个数组执行此操作并比较哪个结果更高。试图同时将两者相互比较是太复杂了。

#4


-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.

您不需要三元运算符。另外,请注意count(),因为如果数组为null,它将失败。它们也需要相同的长度,否则你会收到错误。

if( is_array($value1)  && is_array($value2) && count($value1)==count($value2) ) {
    for ( $i = 0; $i < count($value1); $i++)
    {
        $test[] = ($value1[$i] == $value2[$i]);
    }
}