我需要在PHP中合并两个数组,并且还必须匹配键
这是我的阵列
array1看起来像
[
0 => "num",
1 => "No",
2 => "Name",
3 => "Phone",
4 => "Price",
5 => "Date"
]
array2看起来像
[
0 =>[
"No" => "FR201605832",
"Price" => 199.0,
"Date" => "2016-09-01",
"num" => "19",
"Name" => "Tom",
"Phone" => "0900123456"
],
1 =>[
"No" => "EC2016010100001",
"Price" => 1680.0,
"Date" => "2016-09-01",
"num" => "1680",
"Name" => "Ted",
"Phone" => "0900321654"
]
]
这就是我想要的结果
[
0 =>[
"num" => "19",
"No" => "FR201605832",
"Name" => "Tom",
"Phone" => "0900123456",
"Price" => 199.0,
"Date" => "2016-09-01",
],
1 => [
"num" => "1680",
"No" => "EC2016010100001",
"Name" => "Ted",
"Phone" => "0900321654",
"Price" => 1680.0,
"Date" => "2016-09-01",
]
]
array1是用户对输入的excel标题进行重新排序,我可以获取重新排序请求,但不知道如何与值组合
Plz帮助我合并两个数组并导出预期结果,Thx
这是下一步
按array3重命名Result数组键
array3外观
数组:6 [▼
“ num” =>“ id”
“否” =>“ ECNo”
“名称” =>“用户”
“电话” =>“移动”
“价格” =>“总计”
“日期” =>“发薪日”
]
我尝试了
foreach($ exportdata为&$ val){
$ val [$ values] = $ val [$ keys];
unset($ val [$ value]);
}
但是出现错误非法偏移类型
有什么好主意吗?
答案 0 :(得分:2)
使用array_flip()和array_merge()来获得结果
$new = [];
foreach ($array as $value) {
$new[] = array_merge(array_flip($order),$value);
}
dd($new);
您可以在创建第二个数组时订购该数组。无需再寻求帮助 所以我想你的第二个数组数据有些不同,所以尝试这种方式
$new = [];
foreach ($array as $value) {
if(isset($value['num'])) $data['num'] = $value['num'];
if(isset($value['No'])) $data['No'] = $value['No'];
if(isset($value['Name'])) $data['Name'] = $value['Name'];
if(isset($value['Phone'])) $data['Phone'] = $value['Phone'];
if(isset($value['Price'])) $data['Price'] = $value['Price'];
if(isset($value['Date'])) $data['Date'] = $value['Date'];
$new[] = $data;
}
答案 1 :(得分:1)
这样做:
$modified = [];
foreach($parent as $i => $val) {
foreach($child as $ci => $data) {
$modified[$ci][$val] = $data[$val];
}
}
echo "<pre>";print_r($modified);