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.– Vinícius Lara
Well, in that case just do
explode(',', $_GET['a'])
to transform into array– bfavaretto
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.
– Vinícius Lara
Hm, in the case of arrays with implicit keys the order always makes a difference. I will edit the answer.
– bfavaretto
I’m on hold...
– Vinícius Lara
I edited, @user3163662. See if you can solve it now.
– bfavaretto
would not be
array_values
instead ofsort
, as luck only accepts values passed by reference?– Wallace Maxters
@Wallacemaxters To use the
==
the values and the respective keys need to be equal, so I usedsort
. Thearray_values
would also guarantee this?– bfavaretto
@bfavaretto,
array_values
returns only array values, numerically reordered. In your example, the waysort
used would generate some errors, because this function only accepts values passsados by reference– Wallace Maxters
@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 thesort
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.– bfavaretto