Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中找到多个数组中的最高数组(包含最多项)_Php_Arrays - Fatal编程技术网

如何在PHP中找到多个数组中的最高数组(包含最多项)

如何在PHP中找到多个数组中的最高数组(包含最多项),php,arrays,Php,Arrays,我有多个数组,需要比较它们包含多少项,并选择值最高的数组(=其中项最多的数组) 例如,我有3个阵列: $a_arr = array( '0' => '45', '1' => '50', '2' => '100' ); $b_arr = array( '0' => 'apple' ); $c_arr = array( '0' => 'toyota', '1' => 'ferrari', ); 现在,我需要比较这些数组中的项目数,并选择项目数最多的数组。因此,在

我有多个数组,需要比较它们包含多少项,并选择值最高的数组(=其中项最多的数组)

例如,我有3个阵列:

$a_arr = array( '0' => '45', '1' => '50', '2' => '100' );
$b_arr = array( '0' => 'apple' );
$c_arr = array( '0' => 'toyota', '1' => 'ferrari', );
现在,我需要比较这些数组中的项目数,并选择项目数最多的数组。因此,在这种情况下,最高值为3项的
$a_arr

如何在PHP中以最有效的方式实现这一点?

您可以使用它来确定最长的数组

$longest = max($a_arr, $b_arr, $c_arr);
// Will return the array with highest amount of items ($a_arr)
$longest = max(count($a_arr), count($b_arr), count($c_arr));
// Will return the max number of items (3 in this case; length of $a_arr)
将其与组合以获取最长数组中的项目数量

$longest = max($a_arr, $b_arr, $c_arr);
// Will return the array with highest amount of items ($a_arr)
$longest = max(count($a_arr), count($b_arr), count($c_arr));
// Will return the max number of items (3 in this case; length of $a_arr)
您可以使用来确定最长的数组

$longest = max($a_arr, $b_arr, $c_arr);
// Will return the array with highest amount of items ($a_arr)
$longest = max(count($a_arr), count($b_arr), count($c_arr));
// Will return the max number of items (3 in this case; length of $a_arr)
将其与组合以获取最长数组中的项目数量

$longest = max($a_arr, $b_arr, $c_arr);
// Will return the array with highest amount of items ($a_arr)
$longest = max(count($a_arr), count($b_arr), count($c_arr));
// Will return the max number of items (3 in this case; length of $a_arr)
我提议一种方法:

$a_arr = array( '0' => '45', '1' => '50', '2' => '100' );
$b_arr = array( '0' => 'apple' );
$c_arr = array( '0' => 'toyota', '1' => 'ferrari', );

$result = $a_arr;
$nmax = count($a_arr);

$n = count($b_arr);
if($n > $nmax)
{
  $nmax = $n;
  $result = $b_arr;
}

$n = count($c_arr);
if($n > $nmax)
{
  $nmax = $n;
  $result = $c_arr;
}

//
// compare other arrays...
//

//
// maximal elements: $nmax, $result here is the array with the maximum of items.
//
我提议一种方法:

$a_arr = array( '0' => '45', '1' => '50', '2' => '100' );
$b_arr = array( '0' => 'apple' );
$c_arr = array( '0' => 'toyota', '1' => 'ferrari', );

$result = $a_arr;
$nmax = count($a_arr);

$n = count($b_arr);
if($n > $nmax)
{
  $nmax = $n;
  $result = $b_arr;
}

$n = count($c_arr);
if($n > $nmax)
{
  $nmax = $n;
  $result = $c_arr;
}

//
// compare other arrays...
//

//
// maximal elements: $nmax, $result here is the array with the maximum of items.
//
只是为了好玩:

array_multisort(($c=array_map('count',(compact('a_arr','b_arr','c_arr')))),SORT_DESC,$c);
echo key($c) . ' = ' . current($c);
只是为了好玩:

array_multisort(($c=array_map('count',(compact('a_arr','b_arr','c_arr')))),SORT_DESC,$c);
echo key($c) . ' = ' . current($c);

谢谢,但是不能使用更简单的方法并且不创建额外的数组吗?@user2984974只是稍微摆弄了一下,有更新的答案。难以置信,我将测试它。它应该是
$longest=max(count($a_arr),count($b_arr),count($c_arr)),@GottliebNotschnabel您是对的,这将返回任何数组中最大数量的项。更新了答案。谢谢,但是不能使用更简单的方法并且不创建额外的数组吗?@user2984974只是稍微摆弄了一下,有,请看更新的答案。难以置信,我将测试它。它应该是
$longest=max(count($a_arr),count($b_arr),count($c_arr)),@GottliebNotschnabel您是对的,这将返回任何数组中最大数量的项。更新了答案。虽然费劲,但总的来说是正确的(我实际上假设
max()
函数内部就是这样做的)+1异常但总体正确(我实际上假设
max()
函数内部就是这样做的)+1.