Remove duplicate elements in Array

Asked

Viewed 11,081 times

7

I’m getting an array by JSON, and using the array_unique to remove duplicated values, however, is displaying the following error: <b>Notice</b>: Array to string conversion in <b>/var/www/html/.... </b> on line <b>10</b><br />"

On line 10 is just the array_unique.

I have tried several ways, but I cannot remove these duplicate values.

Someone can help me??

Note: data sent by JSON is being received correctly.

$dados = json_decode(($_POST['dados']));
$dados_arr = array_unique($dados); //linha 10
print_r($dados_arr);
  • 1

    The error is stating that you are trying to turn a string into an array; : $dados_arr[] = $dados and then try to pass the array_unique

  • Places the json structure.

  • 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 )

  • 1

    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!

  • @Thiagoalessandro, just one more additional detail that I won’t add to the answer: You should use the second parameter of json_decode as true, to ensure that you will be returned a array, instead of a stdClass(object).

1 answer

7


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.
  • I thought the problem was a multidimensional array, +1;

  • 1

    @rray, I learned that by asking the "questions I already knew the answer". The guy answered this here http://answall.com/questions/76395/comor-fazer-um-array-de-objetos-ter-apenas-values%C3%Banicos

Browser other questions tagged

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