CopyPastehas never been so tasty!

Arrays Comparison (equality, identity, difference) in PHP

by anonymous

  • 0
  • 0
  • 0
287 views

http://codingvilla.com/arrays-comparison-equality-identity-difference-php-single-article-678.aspx

 


Code:

 

1.    <?php  

2.    function array_equal_identity($array1, $array2) {

3.    echo ($array1==$array2)? 'arrays are equal':'arrays are not equal';

4.    echo "<br/>";

5.    echo ($array1===$array2)? 'arrays are identical':'arrays are not identical';

6.    }

7.    $names1= array('name1' => 'Tahreem', 'name2' => 'Anwar', 'name3' => 'Rizwan', 'name4' => 'Khan');

8.    $names2 = array('name1' => 'Tahreem', 'name2' => 'Anwar', 'name3' => 'Rizwan', 'name4' => 'Khan');

9.    array_equal_identity($names1 ,$names2);

10.  ?>

 

 

Output:

 

arrays are equal
arrays are identical

 

Explanation:

 

One line 7 we have declared array $names1 and initializes it, name1 is index name and value is Tahreem as $name2 is index name and its value is Anwar. Online 8 we have declared $names2 array initialize it.

 

Function is on line 2. We have passed it to array_equal_identity($names1 ,$names2).

 

On line 3 we have checked by equal operator that array1 and array2 have equal number of values.

 

It compare $array1==$array2 if both have same number of values print arrays are equal if not print arrays are not equal.

 

On line 5 we have checked that arrays have equal number of values and same type of values means identical.

Add A Comment: