phparraysmultidimensional-arraymerging-data

Filter out rows rows of a 2d array if not related to rows of another 2d array and merge their data


I have two arrays being built as the result of SQL queries on different databases.

One is coming through formatted as such:

$data = array(
    [$sku] => array(
        ['LocalSKU'] => $sku,
        ['Price'] => $msrp,
        ['Price2'] => $wholesale,
        ['Price3'] => $distributor,
        ['Price4'] => $map
    )
)

The other array is formatted as such:

$matchme = array(
    [0] => array(
        ['entity_id'] => $entity_id,
        ['sku'] => $sku,
        ['type_id'] => $type_id
    )
)

Currently, I can get the individual data to match up via:

echo $matchme[0]['sku'];
echo $matchme[0]['entity_id'];
echo $matchme[0]['type_id'];
echo $data[$matchme[0]['sku']]['Price'];
echo $data[$matchme[0]['sku']]['Price2'];
echo $data[$matchme[0]['sku']]['Price3'];
echo $data[$matchme[0]['sku']]['Price4'];

However, when I try and merge the matching rows in both arrays, I get an empty array. The $data array contains 74 unique $sku, and $matchme is the result of checking those $sku's against a database and returning an array with 61 elements. So, the combined array should have 61 elements with matched pricing data based on the $sku.

How I am attempting to build the combined array is below, can anyone point me towards what I am doing wrong?

foreach ($matchme as $key) {
    if (in_array($matchme[$key]['sku'], $data)) {
        $matched_luggage[$matchme[$key]['sku']][] = array(
            'sku' => $matchme[$key]['sku'],
            'entity_id' => $matchme[$key]['entity_id'],
            'type_id' => $matchme[$key]['type_id'],
            'MSRP' => $data[$matchme[$key]['sku']]['Price'],
            'Wholesale' => $data[$matchme[$key]['sku']]['Price2'],
            'Distributor' => $data[$matchme[$key]['sku']]['Price3'],
            'MAP' => $data[$matchme[$key]['sku']]['Price4']
        );
    }
}

In the above example, evaluate $key as 0, and the value of ['sku'] are matching.

------------------------Edited-------------------------

Per request, here is the result of print_r($data) truncated for space:

Array
(
[12PK-TITANIUM-CR123A] => Array
    (
        [LocalSKU] => 12PK-TITANIUM-CR123A
        [Price] => 11.76
        [Price2] => 10.32
        [Price3] => 0
        [Price4] => 0
    )

[AA-CLAMSHELL] => Array
    (
        [LocalSKU] => AA-CLAMSHELL
        [Price] => 0.25
        [Price2] => 0
        [Price3] => 0
        [Price4] => 0
    )

[AAA-CLAMSHELL] => Array
    (
        [LocalSKU] => AAA-CLAMSHELL
        [Price] => 0.25
        [Price2] => 0
        [Price3] => 0
        [Price4] => 0
    )

[AE-AEL280PI] => Array
    (
        [LocalSKU] => AE-AEL280PI
        [Price] => 0
        [Price2] => 0
        [Price3] => 0
        [Price4] => 0
    ) ) 

Per request, here is the result of print_r($matchme) truncated for space:

Array
(
[0] => Array
    (
        [entity_id] => 693
        [sku] => 12PK-TITANIUM-CR123A
        [type_id] => simple
    )

[1] => Array
    (
        [entity_id] => 2596
        [sku] => AE-AEL480HL
        [type_id] => simple
    )

[2] => Array
    (
        [entity_id] => 2597
        [sku] => AE-AEL600-T6
        [type_id] => simple
    )

[3] => Array
    (
        [entity_id] => 2598
        [sku] => AE-AEWL2
        [type_id] => simple
    ) ) 

Per request, here is the desired result of $matched_luggage:

$matched_luggage = array(
    [12PK-TITANIUM-CR123A] => array(
        [sku] => 12PK-TITANIUM-CR123A,
        [entity_id] => 693,
        [type_id] => simple,
        [Price] => 11.76,
        [Price2] => 10.32,
        [Price3] => 0,
        [Price4] => 0
    )
)

with an additional array per matched sku.


Solution

  • Try this:

    foreach ($matchme as $arrProduct) {
        if (isset($data[$arrProduct['sku']])) {
            $arrMerged[$arrProduct['sku']]=array_merge($arrProduct, $data[$arrProduct['sku']]);
        }
    }
    
    print_r($arrMerged);
    

    The reason your code doesn't work is here:

    if(in_array($matchme[$key]['sku'], $data)) [...]
    

    What in_array() does is tell you whether your needle (in your case the SKU string) exists as a value of array haystack (in your case, $data). You are essentially trying to match a string to an array, rather than another string.

    What you really want is just to match the SKU string to the key of $data, for which isset() is probably the simplest approach.