简体   繁体   English

foreach 循环和 array_merge 不正确匹配

[英]foreach loop and array_merge not matching correctly

I have the following two arrays that I am trying to merge into one where a common order_id is found:我有以下两个数组,我试图将它们合并到一个找到公共 order_id 的数组中:

$orders array: $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 array: $chitchattracking 数组:

[1] => Array (
    [order_id] => 44938
    [carrier_tracking_code] => 9205590221582717655498
    )
[2] => Array (
    [order_id] => 44854
    [carrier_tracking_code] => 92055902215827
    )

in the above array samples there is a match of the order_id: 44938在上面的数组样本中有一个 order_id 匹配:44938

Here is my code that is checking for the match, and put it into a new array $tracked:这是我检查匹配的代码,并将其放入新数组 $tracked 中:

foreach ($orders as $order) {
    if (($key = array_search($order['order_id'], array_column($chitchattracking, 'order_id'))) !== false) {
        $tracked[] = array_merge( $order, $chitchattracking[$key]);
    }
}

somehow i really messed this up and it matched the wrong order_ids and posted the incorrect tracking numbers.不知何故,我真的把它搞砸了,它匹配了错误的 order_ids 并发布了不正确的跟踪号码。 further, when I run the code with this limited amount, it is not even finding the match.此外,当我以有限的数量运行代码时,它甚至找不到匹配项。

Your problem is almost certainly being caused by the fact that array_column returns an array that is numerically indexed starting at 0, regardless of the keys of the input array.您的问题几乎肯定是由array_column返回一个从 0 开始数字索引的数组的事实引起的,而不管输入数组的键是什么 So if the input array is not also numerically indexed starting at 0, the key returned by array_search will not necessarily match a key in the input array (this is why your code won't run at all with the sample data in your question).因此,如果输入数组的数字索引也不是从 0 开始,则array_search返回的键不一定与输入数组中的键匹配(这就是为什么您的代码根本不会使用问题中的示例数据运行的原因)。 The easiest way to work around this is to re-index $chitchattracking by order_id , then you can do a simple isset check to control the push to $tracking :解决此问题的最简单方法是通过order_id重新索引$chitchattracking order_id ,然后您可以进行简单的isset检查以控制对$tracking的推送:

$tracking = array();
$chitchat = array_column($chitchattracking, null, 'order_id');
foreach ($orders as $order) {
    $order_id = $order['order_id'];
    if (isset($chitchat[$order_id])) {
        $tracking[] = array_merge($order, $chitchat[$order_id]);
    }
}
print_r($tracking);

Output:输出:

Array
(
    [0] => Array
        (
            [order_id] => 44938
            [customers_email_address] => test@hotmail.com
            [customers_name] => t
            [carrier_tracking_code] => 9.2055902215827E+21
        )
)

Demo on 3v4l.org 3v4l.org 上的演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM