2

I have 2 arrays:

$array1 = array("1","2","3","4","5");
$array2 = array("7","2","3","1","5");

What I want to do is match the values in $array1 and $array2 strictly in order.

The desired output should be 2, 3 and 5 since 1 is not in the same order. I've tried array_intersect but it only matches array values and not taking account their order.

Thanks in advance.

kmoser
  • 8,780
  • 3
  • 24
  • 40
  • You could have used a loop and then inside compare the values between the arrays 1:1. But there's the built in way in answers. – GetSet Mar 02 '22 at 02:17

1 Answers1

1

You can use array_intersect_assoc as below:

$result = array_intersect_assoc($array1 , $array2 );
sajjad rezaei
  • 945
  • 1
  • 10
  • 23