Vortex
Vortex

Reputation: 816

How to compare two associative arrays by key

I am trying to compare two associative arrays and get the difference based upon the value and also based upon the key. I have tried using an array_filter with a closure

The two arrays are like so:

Array 1

$newArr = [
    0 => [
        'id' => 'UT5',
        'qty' => '4'
    ],
    1 => [
        'id' => 'WRO',
        'qty' => '3'
    ],
    2 => [
        'id' => 'SHO',
        'qty' => '3' 
    ]
];

Array 2

$oldArr = [
    0 => [
        'id' => 'SHO',
        'qty' => '1'
    ],
    1 => [
        'id' => 'UT5',
        'qty' => '2'
    ],
];

My desired output is as follows:

array(3) 
{ 
  ["UT5"]=> int(2) 
  ["SHO"]=> int(2) 
  ["WRO"]=> int(3)
} 

I have gotten this far:

<?php

$newArr = [
    0 => [
        'id' => 'UT5',
        'qty' => '4'
         ],
    1 => [
        'id' => 'WRO',
        'qty' => '3'
    ],
    2 => [
        'id' => 'SHO',
        'qty' => '3'
    ]
];

$oldArr = [
    0 => [
        'id' => 'SHO',
        'qty' => '1'
    ],
    1 => [
        'id' => 'UT5',
        'qty' => '2'
    ],
];

$toAdd = [];
foreach ($newArr as $item) {
    $itemsToAdd = array_walk($oldArr, function ($k) use ($item, &$toAdd) {
        if ($k['id'] == $item['id']) {
            $toAdd[$k['id']] = max($k['qty'], $item['qty']) - min($k['qty'], $item['qty']);
        }
    });
}

var_dump($toAdd); die();

However with this function, my current output is:

array(2) { 
    ["UT5"]=> int(2) 
    ["SHO"]=> int(2) 
}

Note that WRO is missing. Is there a way that I can add a conditional to accurately check for this? I have tried a few solution such as !in_array and else but neither are giving me the desired output.

Any help is appreciated! Thanks!

Upvotes: 0

Views: 995

Answers (2)

splash58
splash58

Reputation: 26153

Make it in one pass

$toAdd = [];
foreach ($newArr as $item) 
   $toAdd[$item['id']] = $item['qty'];
foreach ($oldArr as $item) 
   if (isset($toAdd[$item['id']])) 
       $toAdd[$item['id']] = abs($toAdd[$item['id']] - $item['qty']);
   else 
       $toAdd[$item['id']] = abs($item['qty']);
print_r($toAdd);

Upvotes: 1

Digits
Digits

Reputation: 2684

That's an easy one, your code saves a value ONLY if the key is present in both arrays. Just add a clause to check if the key DOESN'T exist in the old array. (also do the opposite in case the old array has a key the new one doesn't have)

if (!isset(old array [ new array key ]){
$newArray[new array key] = new array key and values;

Your program structure is optimized for the computer and is too complex to follow as a human, I rewrote it entirely.

<?php
$newArr = [0 => ['id' => 'UT5', 'qty' => '4'], 1 => ['id' => 'WRO', 'qty' => '3'], 2 => ['id' => 'SHO', 'qty' => '3']];

$oldArr = [0 => ['id' => 'SHO', 'qty' => '1'], 1 => ['id' => 'UT5', 'qty' => '2'], ];

$newReset = [];
foreach( $newArr as $item ) {
    $newReset[$item['id']] = $item['qty'];
}

$oldReset = [];
foreach( $oldArr as $item ) {
    $oldReset[$item['id']] = $item['qty'];
}

foreach( $newReset as $key => $val ) {
    if( isset( $oldReset[$key] ) ) {
        $toAdd[$key] = max( $oldReset[$key], $val ) - min( $oldReset[$key], $val );
    }
    else $toAdd[$key] = intval($val);
}

var_dump( $toAdd );

And here's the result.

array(3) {
      ["UT5"]=>
      int(2)
      ["WRO"]=>
      int(3)
      ["SHO"]=>
      int(2)
    }

Upvotes: 1

Related Questions