0

I am trying to merge two arrays based on a matching order_id:

Array1:

[0] => Array (
    [order_id] => 44446
    [carrier_tracking_code] => LB399189926US
    )
[1] => Array (
    [order_id] => 42816
    [carrier_tracking_code] => 9205590221582717353066
    )
[2] => Array (
    [order_id] => 44490
    [carrier_tracking_code] => 9205590221582717353080
    )
[3] => Array (
    [order_id] => 44507
    [carrier_tracking_code] => 9205590221582717353073
    )
[4] => Array (
    [order_id] => 44437
    [carrier_tracking_code] => 9205590221582717353042
    )
[5] => Array (
    [order_id] => 44519
    [carrier_tracking_code] => 9205590221582717352939
    )
[6] => Array (
    [order_id] => 44561
    [carrier_tracking_code] => 9205590221582717353028
    )
[7] => Array (
    [order_id] => 42985
    [carrier_tracking_code] => 9205590221582717353035
    )
[8] => Array (
    [order_id] => 42673
    [carrier_tracking_code] => 9205590221582717353097
    )
[9] => Array (
    [order_id] => 40197
    [carrier_tracking_code] => 9205590221582717353059
    )
[10] => Array (
    [order_id] => 44733
    [carrier_tracking_code] => 92055902215827
    )

array2:

[79] => Array (
    [order_id] => 44925
    [customers_email_address] => test@test.com
    [customers_name] => JTest name
    )
[80] => Array (
    [order_id] => 44923
    [customers_email_address] =>  test@test.com
    [customers_name] => Test name
    )
[81] => Array (
    [order_id] => 44917
    [customers_email_address] =>  test@test.com
    [customers_name] => Test name
    )
[82] => Array (
    [order_id] => 44915
    [customers_email_address] => a test@test.com
    [customers_name] => Test name
    )

My goal is to create a third array that matches the order_id column from both and merges based on that.

I have tried array_merge and array_merge_recursive etc but those just add on the values to the end of the array I am tried to merge them into the same array key.

I think i need a foreach loop that checks if order_id = order_id from the second, and if it does push the items to the third array. However my problem is the arrays are not sorted by order_id so they may never be the same since the foreach loop would look through the array in the order of the array if that makes sense.

Sackling
  • 1,780
  • 5
  • 37
  • 71
  • Look into using something like `array_column($array2, null, 'order_id')` to index the second array by the order id and then use a `foreach()`. It may work better with the other array being indexed but have a go. – Nigel Ren Oct 11 '20 at 19:25

1 Answers1

-1

Get the customer index for order_id which maintains the index, i.e:

Array
(
    [79] => 44925
    [80] => 44923
    [81] => 44917
    [82] => 44915
)

Then simply loop over, and use array search by the order_id, to return the customer item, add onto array.

Like this: (Note NONE of the order_ids match customers in your example, so have changed)

<?php
$orders = [
    ['order_id' => 44925, 'carrier_tracking_code' => 'LB399189926US'],
    ['order_id' => 44923, 'carrier_tracking_code' => '9205590221582717353066'],
    ['order_id' => 44490, 'carrier_tracking_code' => '9205590221582717353080'],
];

$customers = [
    79 => ['order_id' => 44490, 'customers_email_address' => 'test@test.com', 'customers_name' => 'JTest name'],
    80 => ['order_id' => 44923, 'customers_email_address' => ' test@test.com', 'customers_name' => 'Test name'],
    81 => ['order_id' => 44925, 'customers_email_address' => ' test@test.com', 'customers_name' => 'Test name']
];

$customer_index = array_combine(array_keys($customers), array_column($customers, 'order_id'));

$result = [];

foreach ($orders as $key => $order) {
    $result[$key] = $order;
    
    $customer = array_search($order['order_id'], $customer_index);
    $result[$key]['customer'] = $customer ? $customers[$customer] : [];
}

print_r($result);

https://3v4l.org/TLYSI

Result:

Array
(
    [0] => Array
        (
            [order_id] => 44925
            [carrier_tracking_code] => LB399189926US
            [customer] => Array
                (
                    [order_id] => 44925
                    [customers_email_address] =>  test@test.com
                    [customers_name] => Test name
                )

        )

    [1] => Array
        (
            [order_id] => 44923
            [carrier_tracking_code] => 9205590221582717353066
            [customer] => Array
                (
                    [order_id] => 44923
                    [customers_email_address] =>  test@test.com
                    [customers_name] => Test name
                )

        )

    [2] => Array
        (
            [order_id] => 44490
            [carrier_tracking_code] => 9205590221582717353080
            [customer] => Array
                (
                    [order_id] => 44490
                    [customers_email_address] => test@test.com
                    [customers_name] => JTest name
                )

        )

)

Ideally, you wouldn't select all customers into an array at runtime due to memory issues and instead do this with a database query.

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106