The PHP Handbook does not detail this in the English version of the function array_unique
, but in the English version, it shows that this function has a second parameter.
See the "skeleton" of this function - taken from PHP manual, array_unique, in English:
array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
The function array_unique
internally uses a comparison method, which by default values are treated as strings
, and that is why the error is generated.
So you have to change this form of comparison. Instead of SORT_STRING
let’s use SORT_REGULAR
.
Example:
$dados = json_decode(($_POST['dados']));
$dados_arr = array_unique($dados, SORT_REGULAR); //linha 10
print_r($dados_arr);
By adding the constant SORT_REGULAR
in the second parameter of array_unique
, it is possible to work with other values, regardless of the type of the same.
We can even use the SORT_REGULAR
for make a collection of unique objects.
For more information, see the possible flags that can be used with array_unique
.
SORT_REGULAR
- compare items normally (does not change types)
SORT_NUMERIC
- compares items numerically
SORT_STRING
- compare items as strings
SORT_LOCALE_STRING
- compares items as strings, based on the current location.
The error is stating that you are trying to turn a string into an array; :
$dados_arr[] = $dados
and then try to pass thearray_unique
– RFL
Places the json structure.
– rray
I tried to change it like Rafaek said, but I still get the same results. Following is part of the structure: Array ( [0] => Array ( [0] => Array ( [0] => 4 [1] => 29/12/1988 [2] => S ) [1] => Array ( [0] => 4 [1] => 29/12/1988 [2] => S ) [2] => Array ( [0] => 7 [1] => [2] => N )
– Thiago Alessandro
See my answer. I ran the test on the command line and it was exactly your error that occurred when I didn’t use the
SORT_REGULAR
!– Wallace Maxters
@Thiagoalessandro, just one more additional detail that I won’t add to the answer: You should use the second parameter of
json_decode
astrue
, to ensure that you will be returned aarray
, instead of astdClass(object)
.– Wallace Maxters