These are my two arrays:
$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"
);
开发者_JAVA技巧
I want to combine them as:
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
)
)
I tried the code below but it did not work:
foreach ($test as $key => $value) {
$merged = array_merge((array) $value, $distance);
}
print_r($merged);
<?php
foreach($test as $index=>$array)
{
$test[$index]['distance'] = $distance[$index]
}
print_r($test);
?>
$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];
}
Something like this should work!
foreach ($test as $key => &$value) {
$value["distance"] = $distance[$key];
}
I think array_merge_recursive does what you need.
EDIT: It does not. :) However, a derivate of it, posted in the array_map_recursive
man page does seem to, see this codepad. I'd be interested to know which is faster over a large dataset.
foreach ($test as &$value)
{
$value['distance'] = array_shift($distance);
}
精彩评论