Identical arrays vs equal arrays

There is a small problem when comparing arrays -- you can find arrays where array_diff comes back empty and yet, the arrays are not identical.

<?php
$array1
= array(
 
'foo' => 'foo',
 
'bar' => 'bar',
);

$array2 = array(
 
'bar' => 'bar',
 
'foo' => 'foo',
);

var_dump(array_diff($array1, $array2)); // Result: empty array.
var_dump($array1 == $array2); // Result: true.
var_dump($array1 === $array2); // Result: false.
?>

The behavior is logical when the arrays (seemingly) have no keys.

<?php
$array1
= array(
 
'foo',
 
'bar',
);

$array2 = array(
 
'bar',
 
'foo',
);

var_dump(array_diff($array1, $array2)); // Result: empty array.
var_dump($array1 == $array2); // Result: false.
var_dump($array1 === $array2); // Result: false.
?>

Since

<?php
$array1
[0] != $array2[0];
?>

but slightly less so when the arrays are associative. In the first example:
<?php
$array1
['foo'] === $array2['foo'];
$array1['bar'] === $array2['bar'];
?>

Moral of the story: be careful with using identical on associative arrays.

Comments

hehe

Does your brain see difference in ordering? it may have same keys and values but ordering is not the same, so not identical. When you do foreach, it will be different first.

There is only one problem with php, a caveman can write php code! Because of that many cavemen write some stuff and after a while they think they are real programmers

Make it stop

you can find arrays where array_diff comes back empty and yet, the arrays are not identical.

Yes, because array_diff "Returns an array containing all the entries from array1 that are not present in any of the other arrays." -- it says nothing about the arrays being identical or not. array_diff can operate on more than one array even!

Dream Vacation Network

I liked the posts and cool layout you have here! I would like to thank you for sharing your experience and the time it took to post!! Two Thumbs up!
dream vacation network

Quality Audit China

This is one of the best blogs I’ve ever read. I m absolutely excited to get to read such a well blog. The amount of data that I get is truly great. This is an good masterpiece. I am truly impressed. I would love to read more of your blogs. Please sustain posting. Keep up the good work.

this is very

this is very nice.

http://pexpipe.myblogsite.com

ladies armani watches

I would like to thank you for sharing your experience and the time it took to post!! Two Thumbs up!

The code is excellent. I like

The code is excellent. I like that a lot. Keep it up!
Flat management

hsa ohio

it may have same keys and values but ordering is not the same, so not identical. When you do foreach, it will be different first.

Objects that do not implement

Objects that do not implement Comparable could then be indexed according to their natural or overridden hash.
handheld barcode scanner

christmas quotes in spanish

There is a little issue when evaluating arrays -- you may discover arrays where array_diff comes back clear and yet, the arrays are not similar. christmas quotes in spanish | christmas quotes for boss

This is useful. If you

This is useful. If you haven't programmed java and need to compare two arrays but forget, this will pop right up in google!
Who the hell wants to keep all this crap in their head anyway? indoor flag poles

An inefficient revenue cycle

An inefficient revenue cycle management and medical billing workflow can cost the average hospital millions in lost revenues. This is why having an electronic medical billing solution with an optimized workflow is vital in order to keep a healthcare business in operation.
Medical Billing Services

certainly enjoyed the way you

certainly enjoyed the way you explore your experience and knowledge of the subject!Keep up on it. Thanks for sharing the info

I enjoyed reading your easy

I enjoyed reading your easy transformations. I see you offer priceless info. Stumbled into this blog by chance but I’m sure glad I clicked on that link. Forexhandel
divorce forms

Article 1

This was a useful post and I think it is rather easy to see from the other comments as well that this post is well written and useful. I bookmarked this blog a while ago because of the useful content and I am never being disappointed. Keep up the good work .laser hair removal machine.

Among the finest that you

Among the finest that you realize that you truly made a excellent examination about it particular subject. This site really is
comprised of an excellent useful connection which will afflicted me with a a lot of knowledge about this kind of change lives.I'll use this information to provide article critique for my friends.

The amount of data that I get

The amount of data that I get is truly great. This is an good masterpiece.There is only one problem with php, a caveman can write php code! Because of that many cavemen write some stuff and after a while they think they are real programmers.modern warfare 3

Arrays store large number of

Arrays store large number of elements.When array comparison is required the array_diff can be used,but sometimes it may give clear output but still both arrays may not be similar...we have to ensure the output is proper...
dog snuggie

It is great to have the

It is great to have the opportunity to read a good quality article with useful information on topics that plenty are interested on
70-298 exam
70-433 exam
1z0-050 exam
BCP-410 exam
642-515 exam
70-238 exam
642-533 exam
1Y0-A18 exam

Great Buddy , Thank you for

Great Buddy , Thank you for sharing information.send flowers to usa

FDDFS

the past few hours and foundiphone external batteryyour post to be well written and has solid informationenvelope printing..!gemstone beads

It is so lucky to read your

It is so lucky to read your blog,it is full of useful message.I wish we both can do better in the future.It great honor if you can visit our website,and give us some suggestion.
70-685 dumps
312-50 dumps
VCP-310 dumps
350-030 dumps
70-432 dumps
70-620 dumps
70-686 dumps
642-642 dumps

Its always important to use

Its always important to use the diff_array because array store large number of elements.its interesting to see a strange result that can come out in case of diff_array is used.I dint come across this program when i was practicsing arrays.
expunge criminal record

Hey there, I am more than

Hey there, I am more than excited on being here, please keep updating, you are one of the very bests..
workers compensation fraud prevention

Arrays store large number of

Arrays store large number of values,so one needs to be know the difference between the arrays when comparing two arrays of same size.But the output may be strange with some of the identical and associative arrays,so one has to be careful with associative and identical arrays.
steroids

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options