-1

I have Two Arrays

Array
(
    [0] => Array
        (
            [option_value_id] => 82
            [product_option_value_id] => 18771
            [quantity] => 25
        )

    [1] => Array
        (
            [option_value_id] => 46
            [product_option_value_id] => 18776
            [quantity] => 5
        )


)

2nd Array

Array
(
    [0] => Array
        (
            [option_value_id] => 82
            [product_option_value_id] => 18771
            [name] => Apricot
            [quantity] => 30
        )

    [1] => Array
        (
            [option_value_id] => 51
            [product_option_value_id] => 18780
            [name] => 2XL
            [quantity] => 5
        )

    [2] => Array
        (
            [option_value_id] => 48
            [product_option_value_id] => 18778
            [name] => L
            [quantity] => 5
        )

    [3] => Array
        (
            [option_value_id] => 46
            [product_option_value_id] => 18776
            [quantity] => 5
        )

)

Now i want What is the Difference between Two Arrays Regardless of its sorting . if their is a difference between two arrays show me true or false The Result should be

Array
(
    [0] => Array
        (
            [option_value_id] => 82
            [product_option_value_id] => 18771
            [name] => Apricot
            [quantity] => 30
        )

    [1] => Array
        (
            [option_value_id] => 51
            [product_option_value_id] => 18780
            [name] => 2XL
            [quantity] => 5
        )

    [2] => Array
        (
            [option_value_id] => 48
            [product_option_value_id] => 18778
            [name] => L
            [quantity] => 5
        )

)

Because in first index quantity is change from 25 to 30 and the other indexes are not pressent in the first array,

user3664519
  • 129
  • 1
  • 5
  • 3
    And what have you tried so far? Show us your code. – JustBaron Feb 23 '17 at 11:28
  • 1
    http://stackoverflow.com/questions/12246039/multidimensional-array-difference-php – user3664519 Feb 23 '17 at 11:30
  • 1
    Possible duplicate of [multidimensional array difference php](http://stackoverflow.com/questions/12246039/multidimensional-array-difference-php) – B. Desai Feb 23 '17 at 11:32
  • 2
    Causing undue bloat on SO by one user asking the same question twice -- poor form. Asking us to chase your code around SO so we can solve your issue is also poor form. – mickmackusa Feb 23 '17 at 11:34

2 Answers2

1

I'm going to make some assumptions here. If they are wrong please edit your question to clarify...

  1. Your second array will never be smaller than your first array.
  2. All sub-arrays in your first array will exist in your second array.
  3. option_value_id values are unique within each array.

    <?php
    /* setup arrays */
    $a=array(
        array("option_value_id"=>82,
              "product_option_value_id"=>18771,
              "quantity"=>25),
        array("option_value_id"=>46,
              "product_option_value_id"=>18776,
              "quantity"=>5)
    );
    $b=array(
        array("option_value_id"=>82,
              "product_option_value_id"=>18771,
              "name"=>"Apricot",
              "quantity"=>30),
        array("option_value_id"=>51,
              "product_option_value_id"=>18780,
              "name"=>"2XL",
              "quantity"=>5),
        array("option_value_id"=>48,
              "product_option_value_id"=>18778,
              "name"=>"L",
              "quantity"=>5),
        array("option_value_id"=>46,
              "product_option_value_id"=>18776,
              "quantity"=>5)
    );
    /* Use `option_value_id` as new key for each sub-array in $a & $b */
    /* If you can do this earlier in your code, you can omit this step */
    foreach($a as $i=>$arr){
        $new_a[$arr["option_value_id"]]=$arr;
    }
    foreach($b as $i=>$arr){
        $new_b[$arr["option_value_id"]]=$arr;
    }
    /* Loop through $b and check for duplicates. */
    foreach($new_b as $b_key=>$b_sub){
        if(isset($new_a[$b_key]) && $b_sub===$new_a[$b_key]){   // exists in $a & dupe
            unset($new_b[$b_key]);
            echo "option_value_id: $b_key was identical and was dropped.<br>";
        }else{
            echo "option_value_id: $b_key is new or changed (unique)<br>";
        }
    }
    echo "<br>";
    echo (sizeof($new_b)?"True, changes were made":"False, no changes");
    

    Output:

    option_value_id: 82 is new or changed (unique)
    option_value_id: 51 is new or changed (unique)
    option_value_id: 48 is new or changed (unique)
    option_value_id: 46 was identical and was dropped.
    
    True, changes were made
    

I deliberately avoided using array_unique() and array_diff() because there are claims they cannot be trusted to use === comparisons. As for the foreach loops, I have read many times that they outperform most php functions that do the same job. If you have speed concerns please benchmark and make an informed decision.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • @user3664519 if this solution meets your satisfaction, please put a green tick on it so others can consider this question sufficiently answered. – mickmackusa Feb 25 '17 at 12:07
0

Use array_diff()function and it will solve your problem

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");

$result=array_diff($a1,$a2);
print_r($result);
?>

Output: Array ( [d] => yellow )

Danish Bhatti
  • 57
  • 1
  • 9