【发布时间】:2021-01-28 09:39:24
【问题描述】:
我有以下两个数组,我试图将它们合并到一个找到共同 order_id 的数组中:
$orders 数组:
[0] => Array (
[order_id] => 45145
[customers_email_address] => test@test.com
[customers_name] => test name
)
[1] => Array (
[order_id] => 45136
[customers_email_address] => test@yahoo.com
[customers_name] => test name
)
[2] => Array (
[order_id] => 45117
[customers_email_address] => test@yahoo.com
[customers_name] => test name
)
[3] => Array (
[order_id] => 44959
[customers_email_address] => test@gmail.com
[customers_name] => test name
)
[4] => Array (
[order_id] => 44938
[customers_email_address] => test@hotmail.com
[customers_name] => t
)
$chitchattracking 数组:
[1] => Array (
[order_id] => 44938
[carrier_tracking_code] => 9205590221582717655498
)
[2] => Array (
[order_id] => 44854
[carrier_tracking_code] => 92055902215827
)
在上面的数组样本中有一个匹配的order_id:44938
这是我正在检查匹配项的代码,并将其放入新数组 $tracked:
foreach ($orders as $order) {
if (($key = array_search($order['order_id'], array_column($chitchattracking, 'order_id'))) !== false) {
$tracked[] = array_merge( $order, $chitchattracking[$key]);
}
}
不知何故,我真的搞砸了,它匹配了错误的 order_ids 并发布了不正确的跟踪号。此外,当我运行这个有限数量的代码时,它甚至找不到匹配项。
【问题讨论】:
-
你的
$chitchattracking数组中没有0键吗?
标签: php arrays for-loop array-merge