这是我的两个阵列:
$test = array(
"0" => array(
"mem_id" => "299",
"profilenam" => "Guys&Dolls",
"photo_b_thumb" => "photos/935a89f58ef2f3c7aaaf294cb1461d64bth.jpeg"
),
"1" => array(
"mem_id" => "344",
"profilenam" => "Dmitry",
"photo_b_thumb" => "no")
);
$distance = array(
"0" => "0",
"1" => "3.362",
"2" => "0.23"
);
我希望将它们组合为:
Array
(
[0] => Array
(
[mem_id] => 299
[profilenam] => Guys&Dolls
[photo_b_thumb] => photos/935a89f58ef2f3c7aaaf294cb1461d64bth.jpeg
[distance] => 3.362
)
[1] => Array
(
[mem_id] => 344
[profilenam] => Dmitry
[photo_b_thumb] => no
[distance] => 0.23
)
)
我尝试了下面的代码,但它不起作用:
foreach ($test as $key => $value) {
$merged = array_merge((array) $value, $distance);
}
print_r($merged);
答案 0 :(得分:2)
<?php
foreach($test as $index=>$array)
{
$test[$index]['distance'] = $distance[$index]
}
print_r($test);
?>
答案 1 :(得分:1)
$test = array("0" => array("mem_id" => "299", "profilenam" => "Guys&Dolls", "photo_b_thumb" => "photos/935a89f58ef2f3c7aaaf294cb1461d64bth.jpeg"
), "1" => array("mem_id" => "344", "profilenam" => "Dmitry", "photo_b_thumb" => "no"));
$distance = array("0" => "0", "1" => "3.362", "2" => "0.23");
foreach( $test as $id => $data ) {
$test[$id]['distance'] = $distance[$id];
}
这样的事情应该有效!
答案 2 :(得分:1)
foreach ($test as $key => &$value) {
$value["distance"] = $distance[$key];
}
答案 3 :(得分:0)
我认为array_merge_recursive可以满足您的需求。
编辑:事实并非如此。 :)然而,在array_map_recursive
手册页中发布的它的衍生物似乎确实如此,请参阅this codepad。我有兴趣知道哪个比大数据集更快。
答案 4 :(得分:-1)
foreach ($test as &$value)
{
$value['distance'] = array_shift($distance);
}