将M * N数组转换为N * M数组PHP

时间:2016-05-04 04:52:18

标签: php arrays sorting arraylist

我在array上使用PHP并发现问题,因为我有一组数组

$x = array(array("Ram","shyam"),array("Hari","Gopal"),array("Rama","Bidhya"))

现在我想把这个数组转换成这样的

array(array("Ram","Hari","Rama"),array("Shyam","Gopal","Bidhya"))

谢谢

1 个答案:

答案 0 :(得分:1)

试试这个

array_unshift()函数将新元素插入到数组中。新数组值将插入数组的开头。

$x = array(array("Ram","shyam"),array("Hari","Gopal"),array("Rama","Bidhya"));

function transpose($array) {
    array_unshift($array, null);
    return call_user_func_array('array_map', $array);
}

$out = transpose($x);
echo '<pre>';
print_r($out);

<强>结果

Array
(
    [0] => Array
        (
            [0] => Ram
            [1] => Hari
            [2] => Rama
        )

    [1] => Array
        (
            [0] => shyam
            [1] => Gopal
            [2] => Bidhya
        )

)