Skip to content
Advertisement

How to check a php array, that contain the same value as other array

i have two array which is $a and $b, where the value of the array is same but the index of elements is different.

$a 
Array ( [0] => there is bald spot on the inside or outside of tyre [1] => uneven tyre wear )
$b
Array ( [0] => uneven tyre wear [1] => there is bald spot on the inside or outside of tyre )

but when i compare by using $a == $b , it return false even when the elements inside the array, is same (just the position of elements is different.)

solutions given before is

$a = Array ( 
    0 => 'there is bald spot on the inside or outside of tyre',
    1 => 'uneven tyre wear'

);

$b = Array ( 
       0 => 'uneven tyre wear', 
       1 => 'there is bald spot on the inside or outside of tyre' 
    );

if(count(array_diff($a,$b)) == 0){
    echo "both array are identical";    

}

but if i remove an elements from $a

$a = Array ( 
    0 => 'there is bald spot on the inside or outside of tyre'
    //1 => 'uneven tyre wear'

);

it still shows the identical if using the solution above.

Advertisement

Answer

use array_diff()

if(count(array_diff($a,$b)) == 0){
    echo "both array are identical";    

}

Output:-https://3v4l.org/vEUiQ

Note:- in case if both array size differs (one have more elements and other have less elements), then you have to put bigger array at first postion while using array_diff()

Check this output:- https://3v4l.org/9lNae

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement