将多维数组值组合成一个数组

时间:2017-08-02 08:34:28

标签: php arrays

我有两个不同的数组,如 -

$csvdata[]   = array("a", "b", "c");
$apendthis[] = array("d", "e", "f");

$result = array_combine($csvdata,$apendthis);
print_r($result);

它给出这样的结果: -

Array ( 
    [Array] => Array ( 
        [0] => d 
        [1] => e 
        [2] => f 
    ) 
)

但我想输出: -

Array ( 
    [Array] => Array ( 
        [0] => a 
        [1] => b 
        [2] => c 
        [3] => d 
        [4] => e 
        [5] => f 
    ) 
)

4 个答案:

答案 0 :(得分:1)

看看这个 -

$final = array();   
$result = array(array_merge($csvdata[0], $apendthis[0]));
foreach($result as $key=>$val)
{
    $final['Array'] = $val;
}
echo "<pre>";
print_r($final);

输出将是,

Array
(
    [Array] => Array
        (
            [0] => a
            [1] => b
            [2] => c
            [3] => d
            [4] => e
            [5] => f
        )

)

答案 1 :(得分:1)

它应该是这样的: -

$result = [];

for ($i = 0; $i < count($csvdata); ++$i) {
    // Catch if 2nd array is shorter
    $arr2 = (isset($apendthis[$i])) ? $apendthis[$i] : [];
    $result[] = array_merge($csvdata[$i], $arr2);
}

// Add the remaining end of the 2nd array if it's longer
if (count($csvdata) < count($apendthis)) {
    $result = array_merge($result, array_slice($apendthis, count($csvdata)));
}

var_dump($result);

答案 2 :(得分:1)

<?php
 $csvdata   = array("a", "b", "c");
 $apendthis = array("d", "e", "f");

$result1 = array_merge($csvdata,$apendthis);
//print_r($result1);

Array                                                                                                                                                                              
(                                                                                                                                                                                  
    [0] => a                                                                                                                                                                       
    [1] => b                                                                                                                                                                       
    [2] => c                                                                                                                                                                       
    [3] => d                                                                                                                                                                       
    [4] => e                                                                                                                                                                       
    [5] => f                                                                                                                                                                       
)        



$result[] = array_merge($csvdata,$apendthis);
print_r($result);

输出:

Array                                                                                                                                                                              
(                                                                                                                                                                                  
    [0] => Array                                                                                                                                                                   
        (                                                                                                                                                                          
            [0] => a                                                                                                                                                               
            [1] => b                                                                                                                                                               
            [2] => c                                                                                                                                                               
            [3] => d                                                                                                                                                               
            [4] => e                                                                                                                                                               
            [5] => f                                                                                                                                                               
        )                                                                                                                                                                          

}
?>

答案 3 :(得分:1)

也许您需要像这样编码:

<?php
$csvdata   = array("a", "b", "c");
$apendthis = array("d", "e", "f");

print_r(array_merge($csvdata,$apendthis));
?>
相关问题