$arr1 = [1,2,3];
$arr2 = [1,2,3,4];
$arr3 = [1,2,3,4,5];
echo max( count($arr1), count($arr2), count($arr3) ); // returns 5
Run Code Online (Sandbox Code Playgroud)
与max我得到的计数,但不知道哪个数组较大.如何获得更大的数组引用($arr3在本例中)?
您应该使用多维数组来存储所有数组,然后遍历多维数组并找到最大的数组,
$lgArraySize = 0; // used for comparing the size of array
$lgArray = array(); // used to store reference of largest array
foreach($arraylist as $array) {
if(count($array) > $largeArraySize) {
$lgArray = &$array;
$lgArraySize = count($array);
}
}
print_r($largeArray);
Run Code Online (Sandbox Code Playgroud)
这是一个班轮代码.(你想要的)
function findMax( $row ){
return count($row);
}
$maxArrayPos = array_search(max( array_map("findMax", $multi )), array_map("findMax", $multi ));
print_r($multi[$maxArrayPos]);
Run Code Online (Sandbox Code Playgroud)
你可以将它作为一个班轮.