Check that all values in one array are part of another

Asked

Viewed 1,305 times

1

I’m working with this code

if(in_array($_GET['a'], array('value1', 'value2', 'value3'))) {
    [...]
}

It serves to check whether $_GET['a'] this inside the array.

I’m trying to pass the values value1,value2,value3 in $_GET['a'] to "check" these values in the array, if all exist it executes my code => [...]

2 answers

3

If I understand the question correctly, and if you really have equal arrays at both ends, you can use the operator ==. He will return true when both arrays have the same keys and values:

array('a' => 10, 'b' => 20) == array('b' => 20, 'a' => 10)

In this example with explicit keys, order is not important. But as in your example the keys are implicit (numerical), you need to sort the arrays first. The example still considers that the input arrives as string separated by commas, as you commented:

$entrada = sort(explode($_GET['a']));
$referencia = sort(array('value1', 'value2', 'value3'))
if($entrada == $referencia) ...

See a test

In that case, you can also use ===, that requires values in arrays to have the same keys and types in the same order (these last two conditions do not apply to ==).

Already the in_array when you receive an array as the first parameter, check whether anyone of its values is contained in the other, and not if all are contained therein (see example in the manual).


Reference: Manual for PHP - Array Operators

  • Greetings! the value that $_GET['a'] received is separated by commas, is not officially an array.

  • Well, in that case just do explode(',', $_GET['a']) to transform into array

  • Let’s see, using the operators, as you mentioned, it will match regardless of the order of the values (I think not) however I am receptive to receive the values in different and random orders.

  • Hm, in the case of arrays with implicit keys the order always makes a difference. I will edit the answer.

  • I’m on hold...

  • I edited, @user3163662. See if you can solve it now.

  • would not be array_valuesinstead of sort, as luck only accepts values passed by reference?

  • @Wallacemaxters To use the == the values and the respective keys need to be equal, so I used sort. The array_values would also guarantee this?

  • @bfavaretto, array_valuesreturns only array values, numerically reordered. In your example, the way sort used would generate some errors, because this function only accepts values passsados by reference

  • @Wallacemaxters O array_values seems to even serve in this case, but I did not find in the manual anything that says that it returns the ordered values (although I have seen this in my tests). Is it guaranteed? About the sort I see no problem. Yes, the array is passed by reference and sorted in-place, but this would not necessarily be a problem for the rest of the author’s code. But I recognize that you can interfere depending on what he wants to do with these arrays afterwards.

Show 5 more comments

2


You can use array_diff to calculate the difference and consider the same or no array.

Being his inputA a string, just use explode to transform into an array and compare the items into arrays. Note that the array order nay influences the result. I put an example in Ideone with 2 cases.

When there is a difference, the output will be an array with key and value array[3]=>value999, if they are equal, even in a different order, the output will be a empty array.

$inputA = explode( ', ' , 'value1, value2, value3, value999' );
$arrays = array( 'value3', 'value1', 'value2' );
print_r( array_diff( $inputA , $arrays ) );

DOC: array_diff( array $array1 , array $array2 [, array $ ... ] )

Returns a array containing all array1 entries that are not present in any of the other arrays.

  • Good, in this case it is much simpler than my suggestion

  • @bfavaretto, just another way :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.