2

I have multidimensional array:-

$first= array( [51581481]=>array(
             'title' => 'Nike - L',
             'price' => '300.00',
             'vendor' => 'Vicky Fashion Point',
             'quantity' => -23,
            ),
        [45747894]=>array(
              'title' => 'Honor Band A (Black) - Default Title',
              'price' => '2249.00',
              'vendor' => 'Honor',
              'quantity' => 8,
            )
        );
$second=array(0 => '45747894',
             1 => '713776113',
            );

I want to compare both array and get difference data from array first. I am using array_diff function

$arr_diff= array_diff($first, $second);

This Error show:-

ERROR: Array to string conversion 
vicky793
  • 431
  • 1
  • 3
  • 17
  • array_filip one of the arrays? – Robert Sep 13 '18 at 14:03
  • 1
    Possible duplicate of [PHP: How to compare keys in one array with values in another, and return matches?](https://stackoverflow.com/questions/25472252/php-how-to-compare-keys-in-one-array-with-values-in-another-and-return-matches) – Michel Sep 13 '18 at 14:06
  • @Michel it's not duplicate. In answer you mention there's intersection and here OP wants to have difference. – Robert Sep 13 '18 at 21:24

3 Answers3

1

Like that

$arr_diff  = array_diff_key($first, array_flip($second));

the trick is to array_flip the second array and use array_diff_key

Working example

$first = array(
    51581481 => array(
        'title' => 'Nike - L',
        'price' => '300.00',
        'vendor' => 'Vicky Fashion Point',
        'quantity' => -23,
    ),
    45747894 => array(
        'title' => 'Honor Band A (Black) - Default Title',
        'price' => '2249.00',
        'vendor' => 'Honor',
        'quantity' => 8,
    ),
);
$second = array(
    0 => 45747894,
    1 => 713776113,
);


var_dump(array_diff_key($first, array_flip($second))); 
Robert
  • 19,800
  • 5
  • 55
  • 85
0

As a simple solution, loop through first array & check if the key exists in second array to get the difference.

$diff = [];

foreach ($first as $key => $value) {
    if(!in_array($key, $second)) {
        $diff[$key] =  $value;
    }
}
Saumini Navaratnam
  • 8,439
  • 3
  • 42
  • 70
TsV
  • 629
  • 4
  • 7
0

You can use array_search() and unset() to diffirenciate two arrays.

$first= array( "51581481"=>array(
             'title' => 'Nike - L',
             'price' => '300.00',
             'vendor' => 'Vicky Fashion Point',
             'quantity' => -23,
            ),
        "45747894"=>array(
              'title' => 'Honor Band A (Black) - Default Title',
              'price' => '2249.00',
              'vendor' => 'Honor',
              'quantity' => 8,
            )
        );
$second=array("0" => '45747894',
             "1" => '713776113',
            );

$array_diff = [];
foreach($first as $key => $val)
{
    if(array_search($key, $second) !== false)
    {
        unset($first[$key]);//remove matched key record
    }
}

echo '<pre>';print_r($first);echo '</pre>';

expected output:

Array
(
    [51581481] => Array
        (
            [title] => Nike - L
            [price] => 300.00
            [vendor] => Vicky Fashion Point
            [quantity] => -23
        )

)
Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51
  • @ Gufran Hasan your answer also right. but when we use foreach loop then code execution consume more time. so we use array function for less consuming executtion time. – vicky793 Sep 13 '18 at 17:32