Skip to content

Instantly share code, notes, and snippets.

@kallefrombosnia
Created March 13, 2020 19:34
Show Gist options
  • Save kallefrombosnia/c4091cf767b88a722118fef9af7e9f18 to your computer and use it in GitHub Desktop.
Save kallefrombosnia/c4091cf767b88a722118fef9af7e9f18 to your computer and use it in GitHub Desktop.
Check if 2 arrays are equal
<?php
$firstArray = [
'ID' => '57ef1717-b222-57b651',
'session' => [
'sessionID' => '8c776739-276059',
'timestamp' => '2020-03-13Z',
'place' => '/'
]
];
$secondArray = [
'ID' => '57ef1717-b222-57b651',
'session' => [
'sessionID' => '8c776739-276059',
'timestamp' => '2020-03-13Z',
'place' => 'home'
]
];
// Check if key => values are same in two arrays with == operator
if($firstArray == $secondArray){
echo 'First array is equal to second array with key -> values.';
}else{
echo 'First array is not equal to second array with key -> values.';
}
// Check if key => value, type and array item order are same in two arrays with === operator
if($firstArray === $secondArray){
echo 'First array is equal to second array with key -> values, type and item order.';
}else{
echo 'First array is not equal to second array with key -> values, type and item order.';
}
// Also its possible to revert logic with
// Check if key => values are not same in two arrays with != operator
if($firstArray != $secondArray){
echo 'First array is not equal to second array with key -> values, type and item order.';
}else{
echo 'First array is equal to second array with key -> values, type and item order.';
}
// Check if key => value, type and array item order are not same in two arrays with !== operator
if($firstArray === $secondArray){
echo 'First array is not equal to second array with key -> values, type and item order.';
}else{
echo 'First array is equal to second array with key -> values, type and item order.';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment