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

标签 php arrays merge transpose

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

我知道如何使用简单的循环来做到这一点,但我想知道是否有更快/内置的方法来做到这一点。这是一个使用循环执行此操作的示例,以便您可以理解我正在寻找的内容。

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);

关于php - 将多个单维数组组合并转置为单个多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8870992/

相关文章:

intellij-idea - 我可以将 IntelliJ 差异查看器/合并工具配置为 tortoiseSVN 中的外部工具吗?

php - mysql 中主表(联合)及其左表的计数、求和

ruby - 如何创建从多个数组中选择的单个元素的每个组合?

php - 将多个表连接到一个查询中

javascript - 获取特定 id 上对象数组中的对象索引 - javascript 或 jQuery

php - 如何用它的值替换数组键

svn - 如何更正 "Commit Failed. File xxx is out of date. xxx path not found."

git merge 仅 merge 最后一次提交

php - 旋转木马控制停止工作

php - 如何使用mysql限制数据库中的所有列