将多个一维数组组合并转置为单个多维数组


Combine and transpose multiple single-dimensional arrays into a single multi-dimensional array

我有三个一维数组,我需要将它们组合成一个三维数组,其中新数组中的每个数组都包含三个原始数组中的一个元素。

我知道如何使用一个简单的循环来完成这项工作,但我想知道是否有一种更快/内置的方法来实现这一点。这里有一个循环的例子,这样你就可以理解我在寻找什么。

function combineArrays(array $array1, array $array2, array $array3) {
    //Make sure arrays are of the same size
    if(count($array1) != count($array2) || count($array2) != count($array3) || count($array1) != count($array3)) {
        throw new Exception("combineArrays expects all paramters to be arrays of the same length");
    }
    //combine the arrays
    $newArray = array();
    for($count = 0; $count < count($array1); $count++) {
        $newArray[] = array($array1[$count], $array2[$count], $array3[$count]);
    }
    return $newArray;
}
$result = array_map(null,$array1,$array2,$array3);