将几个ARRAY更改为新ARRAYS

时间:2014-04-30 05:09:16

标签: php arrays sorting

我想知道如何将几个ARRAYS中的内容更改为新的ARRAYS。 我有这3个变量,每个都有一个ARRAY,让我们说第一个var是$number并且它有这个数组:

Array
(
    [0] => 1
    [1] => 3
    [2] => 9
)

第二个变量是$item,它有:

Array
    (
        [0] => house
        [1] => car
        [2] => bike
    )

第三个是$color,它有:

Array
    (
        [0] => red
        [1] => white
        [2] => black
    )

如何更改内容并创建如下的新数组:

Array
    (
        [0] => 1
        [1] => house
        [2] => red
    )

Array
    (
        [0] => 3
        [1] => car
        [2] => white
    )

Array
    (
        [0] => 9
        [1] => bike
        [2] => black
    )

2 个答案:

答案 0 :(得分:2)

您可以使用array_map

<?php
$number = [1,3,9];
$item = ['house','car','bike'];
$color = ['red','white','black'];
$res = array_map(null, $number, $item, $color);
print_r($res);
?>

将输出您想要的单个数组数组:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => house
            [2] => red
        )

    [1] => Array
        (
            [0] => 3
            [1] => car
            [2] => white
        )

    [2] => Array
        (
            [0] => 9
            [1] => bike
            [2] => black
        )

)

答案 1 :(得分:1)

您可以使用array_map()创建一个回调函数,将每个值一起返回:

$result = array();
function merge_arrays($a,$b,$c){
    return array($a,$b,$c); 
}
$result = array_map("merge_arrays",$number,$item,$color);

<强> DEMO