1

Quick question about PHP associative arrays.

Say there are two arrays:

$A = array("AAA" => "45", "FFF" => "108", "GGG" => "15"); 

and

$B = array("FFF" => "108", "GGG" => "15", "AAA" => "45");

Are these arrays equal? Does the position of an entry in associative arrays matter?

fubar
  • 16,918
  • 4
  • 37
  • 43
Rat2good
  • 119
  • 9
  • 1
    Depends how you compare them. If you take the order into account then no. – Rizier123 Jun 17 '16 at 19:48
  • 2
    It's an easy test: https://3v4l.org/KbV2n – Jonathan Kuhn Jun 17 '16 at 19:50
  • I did not know that var_dump can be used this way: `var_dump($A == $B);` – Rat2good Jun 17 '16 at 19:59
  • 1
    To people who are down-voting this. It isn't a terrible question. PHP is so inconsistent its probably something many people legitimately don't know. – Cfreak Jun 17 '16 at 20:02
  • @Cfreak this is clearly written in a manual. But instead of reading a manual people come here and post yet another question. – u_mulder Jun 17 '16 at 20:14
  • @u_mulder I do not understand why on this forum there are such restrictions. Yes, it is easy to me and many others to ask question instaead of searching information in the coumentation not beeng sure that the information is available. Than is advantage of forums. That is why the forums should exist for. – Rat2good Jun 19 '16 at 18:05
  • @u_mulder another day I asked for clarification on existing topic on this forum, and my question was disabled by moderator... That is the topic: http://stackoverflow.com/questions/13289751/cron-job-in-a-different-timezone And that was my question: "@Bartosz Kowalczyk, could please clarify in you answer "TZ=UTC" is it what should bi inside of file named "cron" in /etc/default/ ?" What is wrong with my question? This kind of forums shoul make our life easyer not opposit... – Rat2good Jun 19 '16 at 18:13
  • Please open http://stackoverflow.com/tour and see `This site is all about getting answers. It's not a discussion forum. There's no chit-chat.` – u_mulder Jun 19 '16 at 18:15

1 Answers1

6

According to PHP official document:

http://php.net/manual/en/language.operators.array.php

Equality:

$a == $b TRUE if $a and $b have the same key/value pairs.

Identity:

$a === $b TRUE if $a and $b have the same key/value pairs in the same order and of the same types.

Demo:

$A=array ("AAA"=>"45", "FFF"=>"108", "GGG"=>"15");
$B=array ("FFF"=>"108", "GGG"=>"15", "AAA"=>"45");

var_dump($A==$B);

bool(true)

var_dump($A===$B);

bool(false)

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
Alex
  • 715
  • 5
  • 14