1

I have this array (decoded from JSON, output with print_r):

stdClass Object
(
    [data] => Array
        (
            [0] => stdClass Object
                (
                    [item] => te
                    [date] => 13.10
                )
            [1] => stdClass Object
                (
                    [item] => te
                    [date] => 13.10
                )
            [2] => stdClass Object
                (
                    [item] => tr
                    [date] => 13.10
                )
        )
)

But now I have to remove all the duplicates. If I try $result = array_unique($array, SORT_REGULAR); $result is null.

Can someone spot my mistake?

1
  • 1
    this is a standard object not an array, convert it in the array first Commented Oct 13, 2017 at 13:50

1 Answer 1

3

This is a stdClass object, not an array. When you decode by using the function json_decode, you need to pass the parameter "true" to have an array:

$array = json_decode($json, true);

Edit: As people noticed in the comments, the actual array exists in $array['data'], so the array_unique must be applied on $array['data'] instead of $array.

Sign up to request clarification or add additional context in comments.

4 Comments

Maybe also add that after that the array_unique must be applied on $array["data"] instead of $array.
@trincot yeah, correct. the actual array exist in $array['data']. please update the answer
So, with the true parameter I can see the sorted array. But how can I get it back to original JSON with the data array?
@Francis You can use the json_encode function: php.net/manual/en/function.json-encode.php

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.