Reputation: 385
This is my array
[51] => Array
(
[171] => 34
[170] => Adult
[168] => 32
[quantity] => 1
)
[52] => Array
(
[171] => 34
[170] => Adult
[168] => 32
[quantity] => 1
)
Now if all the keys and value except quantity are same then the quantity will be added.
ex:
[51] => Array
(
[171] => 34
[170] => Adult
[168] => 32
[quantity] => 2
)
Is there any way to search with dynamic keys. Please help.
Upvotes: 0
Views: 123
Reputation: 18584
Suppose that
$arr1 = Array(
171 => 34
170 => Adult
168 => 32
'quantity' => 1
);
$arr2 = Array(
171 => 34
170 => Adult
168 => 32
'quantity' => 1
);
you could do the following:
$tmp1 = $arr1;
unset($tmp1['quantity']);
$tmp2 = $arr2;
unset($tmp2['quantity']);
if($tmp1 == $tmp2) {
// do what you like
}
when you do $tmp1 == $tmp2
it compares the two arrays by key and value, see http://php.net/manual/en/language.operators.array.php
$a == $b TRUE if $a and $b have the same key/value pairs.
$a === $b TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
Upvotes: 5
Reputation: 4936
You can use array_diff_assoc() function ... it checks array based on key
<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
print_r($result);
?>
Upvotes: 1