Php 如果数组值在另一个数组中重复,请合并两个数组

Php 如果数组值在另一个数组中重复,请合并两个数组,php,arrays,Php,Arrays,我有几个数组,我想检查一个重复的值,如果发现该值在另一个数组中重复,那么将这两个数组组合在一起 下面我举了一个例子,其中有两个数组具有重复的值 示例:以下两个数组中的采购订单号中的值相同。它们不是唯一的值。但是跟踪\u number中的值是唯一的 我想检查purchase\u order\u number中的值是否在另一个数组中重复。如果在另一个数组中找到相同的值,则将这两个数组合并为一个数组 我试图将tracking\u number和service中的值合并到一个数组中,而purchase\

我有几个数组,我想检查一个重复的值,如果发现该值在另一个数组中重复,那么将这两个数组组合在一起

下面我举了一个例子,其中有两个数组具有重复的值

示例:以下两个数组中的
采购订单号
中的值相同。它们不是唯一的值。但是
跟踪\u number
中的值是唯一的

我想检查
purchase\u order\u number
中的值是否在另一个数组中重复。如果在另一个数组中找到相同的值,则将这两个数组合并为一个数组

我试图将
tracking\u number
service
中的值合并到一个数组中,而
purchase\u order\u number
中的值在两个或多个数组中相同

下面是示例数组

未合并

    array (
      'data' => 
      array (
        15 => 
        array (
          'type' => 'Tracking',
          'id' => 2830143,
          'attributes' => 
          array (
            'tracking_number' => '1Z5270560360309870',
            'service' => 'UPS',
            'order_id' => 2606218,
            'purchase_order_number' => '7249491',
            'recipient_attempts' => 1,
          ),
        ),
        16 => 
        array (
          'type' => 'Tracking',
          'id' => 2830144,
          'attributes' => 
          array (
            'tracking_number' => '1Z5270560361740866',
            'service' => 'UPS',
            'order_id' => 2606218,
            'purchase_order_number' => '7249491',
            'recipient_attempts' => 1,
          ),
        ),
      ),
    )
    array (
      'data' => 
      array (
        16 => 
        array (
          'type' => 'Tracking',
          'id' => 2830144,
          'attributes' => 
          array (
            'tracking' => 
            array (
              0 => 
              array (
                'tracking_number' => '1Z5270560360309870',
                'service' => 'UPS',
              ),
              1 => 
              array (
                'tracking_number' => '1Z5270560361740866',
                'service' => 'UPS',
              ),
            ),
            'order_id' => 2606218,
            'purchase_order_number' => '7249491',
            'recipient_attempts' => 1,
          ),
        ),
      ),
    )
下面给出了我需要如何组合上述两个阵列的示例

组合

    array (
      'data' => 
      array (
        15 => 
        array (
          'type' => 'Tracking',
          'id' => 2830143,
          'attributes' => 
          array (
            'tracking_number' => '1Z5270560360309870',
            'service' => 'UPS',
            'order_id' => 2606218,
            'purchase_order_number' => '7249491',
            'recipient_attempts' => 1,
          ),
        ),
        16 => 
        array (
          'type' => 'Tracking',
          'id' => 2830144,
          'attributes' => 
          array (
            'tracking_number' => '1Z5270560361740866',
            'service' => 'UPS',
            'order_id' => 2606218,
            'purchase_order_number' => '7249491',
            'recipient_attempts' => 1,
          ),
        ),
      ),
    )
    array (
      'data' => 
      array (
        16 => 
        array (
          'type' => 'Tracking',
          'id' => 2830144,
          'attributes' => 
          array (
            'tracking' => 
            array (
              0 => 
              array (
                'tracking_number' => '1Z5270560360309870',
                'service' => 'UPS',
              ),
              1 => 
              array (
                'tracking_number' => '1Z5270560361740866',
                'service' => 'UPS',
              ),
            ),
            'order_id' => 2606218,
            'purchase_order_number' => '7249491',
            'recipient_attempts' => 1,
          ),
        ),
      ),
    )
下面是一个基于reduce的解决方案,其中有一些注意事项:

$arr['data'] = array_reduce($arr['data'], function ($out, $item) {
    // keys we want to extract
    static $tracking_keys = ['tracking_number' => '', 'service' => ''];
    // yank tracking keys from attributes
    $tracking = array_intersect_key($item['attributes'], $tracking_keys);
    $item['attributes'] = array_diff_key($item['attributes'], $tracking_keys);
    // insert to new array based on order number
    $order_no = $item['attributes']['purchase_order_number'];
    if (!isset($out[$order_no])) {
        $item['attributes']['tracking'] = [$tracking];
        $out[$order_no] = $item;
    } else {
        array_push($out[$order_no]['attributes']['tracking'], $tracking);
    }
    return $out;
}, []);

'data'
中的键不保留,并且
'id'
由第一项设置。

不再搜索此项的答案。