Home > database >  PHP divide multidimensional array into two part with comparing another array
PHP divide multidimensional array into two part with comparing another array

Time:04-27

I have two multidimensional arrays as follows:

Array 01 for new prices of items:

Array
(
  [42] => Array
    (
        [0] => 110
        [1] => 0.00
    )

  [41] => Array
    (
        [0] => 80
        [1] => 70.00
    )

  [43] => Array
    (
        [0] => 70
        [1] => 60
    )

  [40] => Array
    (
        [0] => 90
        [1] => 80
    )

)
1

Array 02 for old prices of items:

Array
(
  [42] => Array
    (
        [sales_price] => 100.00
        [our_price] => 0.00
    )

  [41] => Array
    (
        [sales_price] => 80.00
        [our_price] => 0.00
    )
)
1

Array key of both array are item ids (42,41,43,40). Now I need to compare $new and $old arrays to create update and insert queries.

If the values in the new array are different from those in the old one, the table should be updated. If there are more elements in the new array than in the old array, they should be identified for the insert query.

So basically I want to divide new array into two parts with comparing old array and considering the terms above.

Expecting two arrays should be as follows:

Array
(
  [42] => Array
    (
        [0] => 110
        [1] => 0.00
    )

  [41] => Array
    (
        [0] => 80
        [1] => 70.00
    )
)
1

Array
(
  [43] => Array
    (
        [0] => 70
        [1] => 60
    )

  [40] => Array
    (
        [0] => 90
        [1] => 80
    )

)
1

This is the code I have so far:

foreach ($priceNew as $newIds => $newPrices) {
  foreach($priceOld as $oldIds => $oldPrices) { 

  }
}

Can anybody help me to divid my new array?

CodePudding user response:

This data has been changed in such a way that an element with key 41 from new matches that from old.

$priceNew = [
    42 => [ 110, 0.00 ],
    41 => [ 80, 70.00 ],
    43 => [ 70, 60 ],
    40 => [ 90, 80 ],
];

$priceOld = [
    42 => [
        'sales_price' => 100.00,
        'our_price' => 0.00
    ],
    41 => [
        'sales_price' => 80.00,
        'our_price' => 70.00
    ],
];

You just have to compare the keys. This can be done with the internal PHP functions array_intersect_key and array_diff_key. The update array is still filtered to meet special conditions.

$updateArr = array_intersect_key($priceNew, $priceOld);

$filter = function($v,$k) use($priceOld){
  return array_values($priceOld[$k]) != $v;
};

$updateArr = array_filter($updateArr, $filter, ARRAY_FILTER_USE_BOTH );
$otherArr = array_diff_key($priceNew,$updateArr);  //added or remained

CodePudding user response:

I think I understand what you're saying enough to produce this for PHP >8...

$priceNew = [
    42 => [ 110, 0.00 ],
    41 => [ 80, 70.00 ],
    43 => [ 70, 60 ],
    40 => [ 90, 80 ],
];

$priceOld = [
    42 => [
        'sales_price' => 100.00,
        'our_price' => 0.00
    ],
    41 => [
        'sales_price' => 80.00,
        'our_price' => 0.00
    ],
];

$updateItems = [];
$insertItems = [];

foreach( $priceNew as $id => $values ) {

    $old = $priceOld[ $id ] ?? null;

    if ( !$old ) {
        $insertItems[ $id ] = $values;
    } else {

        // Not sure if this is the correct conditions, might need to change.
        if ( $values[ 0 ] !== $old[ 'sales_price' ] || $values[ 1 ] !== $old[ 'our_price' ] ) {
            $updateItems[ $id ] = $values;
        }
    }

}
  • Related