Thoroughly comparing two arrays.

Hi all,

I would like to implement the following idea, but so far all the code I have written for it feels too long, not elegant, and, frankly, stupid.

Alright, so I have a list of items and my goal is to know if a new item is already present in that list or not. The items themselves are arrays with various elements. For argument’s sake, what i have is similar to:
[php]
$item = array(“name” => “pizza”, “size” => “large”);
$list = array(
array(“name” => “pizza”, “size” => “large”, “extra” = “cheese”),
array(“name” => “calzone”, “size” => “medium”)
)
[/php]
Now my problem is that if I use [php]array_diff($item, $list[0]); [/php] it will return an empty string, so I have to do the opposite check: [php]array_diff($list[0], $item);[/php] Now it will return a non-empty array with the key-value pairs extra cheese. However, performing these two checks seems somewhat abusrd. Any suggestions? Thanks!

Hi,

try array_unique function. its inbuilt function. for more detail refer this link “http://php.net/manual/en/function.array-unique.php

Sponsor our Newsletter | Privacy Policy | Terms of Service