如何使用php将这两个数组合并为一个数组?

时间:2015-06-04 06:42:53

标签: php arrays

鉴于这两个阵列:

bundle install

我尝试将它合并为

$name=array("alice","ken","wendy");

$frequent=array(3,6,9);                                       

但它失败了。有人可以帮忙吗?

我想要这个:

$data = array($name=>$frequent);

5 个答案:

答案 0 :(得分:4)

您可以使用array_combine

$combined_array = array_combine($name, $frequent);

此处的文档:http://php.net/manual/en/function.array-combine.php

答案 1 :(得分:4)

您可以将array_combine功能用作

语法:

$name=array("alice","ken","wendy"); 
$frequent=array(3,6,9);
$result = array_combine($name,$frequent);

所以你的就像

Array
(
    [alice] => 3
    [ken] => 6
    [wendy] => 9
)

<强>输出

<br>
<div id="about_me">
     This Text color will be change
</div>e here

答案 2 :(得分:2)

像这样写SELECT regr_slope(pd.avg_velocity, pd.load_kg) FROM push_data

答案 3 :(得分:1)

如果你想手动完成。

<?php
    $name=array("alice","ken","wendy");
    $frequent=array(3,6,9);
    $combined=array();
    for($i=0; $i<3; $i++)
    {
        $combined[$name[$i]]=$frequent[$i];
    }
    var_dump($combined);
?>

答案 4 :(得分:1)

[akshay@localhost tmp]$ cat test.php
<?php

$name=array("alice","ken","wendy");
$frequent=array(3,6,9);

// One easy way is
print_r(  array_combine($name, $frequent) );


// Another lengthy way
while ( ($key = array_shift($name)) && ($value = array_shift($frequent)) )
{
    $combined[$key] = $value;
}

print_r( $combined );

?>

<强>输出

[akshay@localhost tmp]$ php test.php
Array
(
    [alice] => 3
    [ken] => 6
    [wendy] => 9
)
Array
(
    [alice] => 3
    [ken] => 6
    [wendy] => 9
)