1

I have two arrays and I want to compare the first with the second array. I have an IF statement and it currently does two checks:

  1. It checks to see if there are any rows from my query
  2. If the above passes, then I want to traverse the array values of the first array and compare it with the second array. I want to traverse through all the names first array (index 0-9) and compare it with the first name of the second array (index 0). Keep doing the same thing until we've compared all the indices 0-9 on the first array with all indices 0-9 of the second array.
  3. Finally, if there is a match I want to exit my PHP script, else if there is no match I continue to do to stuff.

I have tried using the in_array() function and that works, but only with a single variable. Something like this in_array($firstArray[0], $secondArray) works. Something like this:

    if(mysqli_num_rows($result) > 0 && in_array($firstArray[0], $secondArray)){
        exit("exiting now!");
    } 
    else {
        echo "continue to do stuff...";
    }

However, when I put the whole array in the inArray, it won't work. Like this:

in_array($firstArray, $secondArray)

How can I achieve this?

2
  • You state: "Something like this in_array($firstArray[0], $secondArray) works." --- But then state: "However, when I put the whole array in the inArray, it won't work. Like this: in_array($firstArray[0], $secondArray)" - There's something I'm not grasping and there's something you're probably not showing us/telling us and that would be the query itself and/or the value of the arrays. Edit: This comment was based on the original post stackoverflow.com/revisions/46333586/1 Commented Sep 21, 2017 at 0:28
  • @Fred-ii- Sorry, i updated my code. Commented Sep 21, 2017 at 0:31

2 Answers 2

2

You can use array_intersect if you want to know if they intersect, you can use array_diff to see if the two arrays have any differences.

if(mysqli_num_rows($result) > 0 && !array_diff($firstArray, $secondArray)){
       exit("exiting now!");
} else {
       echo "continue to do stuff...";
}

Example:

<?php
$array1 = array (1,2,3,4);
$array2 = array (3,2,1,5);
$array3 = array_diff($array1, $array2);
$array4 = array_intersect($array1, $array2);
echo "<pre>";
echo "Array 1\n ";
print_r($array1);
echo "Array 2\n ";
print_r($array2);

echo "Arrays Difference \n ";
print_r($array3);
echo "Arrays intersect \n ";
print_r($array4);
echo "</pre>";
?>

Output:

Array 1
 Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)
Array 2
 Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 5
)
Arrays Difference 
 Array
(
    [3] => 4
)
Arrays intersect 
 Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Sign up to request clarification or add additional context in comments.

7 Comments

Does it return a boolean true or false that can be used in my conditional?
Did you click on the links? It gives proper example. You can use if (array_diff($blah, $lbah))
The return value is not a boolean.
Returns an array containing all the entries from array1 that are not present in any of the other arrays. So in order to use it in a conditional i will need to create my own function right?
In PHP, an empty array evaluates to false. so !array_diff means they have the same content. Booleans in php
|
1

I think you are looking for array_intersect() which returns the values that are common in two (or more) arrays. http://php.net/manual/en/function.array-intersect.php

It returns an array but if it returns an empty array then empty arrays are falsy.

1 Comment

Does it return a boolean true or false that can be used in my conditional?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.