[英]How to merge these two array?
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"
);
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. 我认为array_merge_recursive可以满足您的需求。
EDIT: It does not. 编辑:它没有。 :) However, a derivate of it, posted in the
array_map_recursive
man page does seem to, see this codepad . :)但是,派生到
array_map_recursive
手册页中的它的array_map_recursive
似乎确实可以看到此代码键盘 。 I'd be interested to know which is faster over a large dataset. 我想知道在大型数据集上哪个更快。
foreach ($test as &$value)
{
$value['distance'] = array_shift($distance);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.