[{
"title" = > "CEO",
"name" = > "George",
"columns" = > [{
"display_name" = > "Salary",
"value" = > "3.85",
}, {
"display_name" = > "Bonus",
"value" = > "994.19",
}, {
"display_name" = > "Increment",
"value" = > "8.15",
}]
}]
data2 = [{
"title" = > "CEO",
"name" = > "George",
"columns" = > [{
"display_name" = > "Address",
"value" = > "Albany",
}, {
"display_name" = > "Phone",
"value" = > "47123",
}, {
"display_name" = > "Mobile",
"value" = > "784123",
}]
}]
我有上面两个数组,如果两个散列中都满足name = george
这样的条件,我想将columns
中的值连接到一个数组中。
所需输出如下:
[{
"title" = > "CEO",
"name" = > "George",
"columns" = > [{
"display_name" = > "Salary",
"value" = > "3.85",
}, {
"display_name" = > "Bonus",
"value" = > "994.19",
}, {
"display_name" = > "Increment",
"value" = > "8.15",
}, {
"display_name" = > "Address",
"value" = > "Albany",
}, {
"display_name" = > "Phone",
"value" = > "47123",
}, {
"display_name" = > "Mobile",
"value" = > "784123",
}]
}]
"name"=>"George"
-可能是标识符吗?还是火柴?两者中的id名称相同,然后将列的值转储到联合数组哈希中。
为您制作了一个函数:
<?php
function array_merge_on_key($original, $new, $key){
if(is_array($original) && is_array($new)){
$copy_from = array();
if(array_key_exists($key, $new)){
if(is_array($new[$key])){
$copy_from = $new[$key];
}
}
return is_null($copy_from) ? array_merge($original[$key], $copy_from) : $original;
}
return false;
}