2

Below is the result and I want to remove duplicate from the array

I tried using this code: $login_data1['items'] = array_values(array_map("unserialize", array_unique(array_map("serialize", $login_data1['items']))));

{
    "items": [
        {
            "id": "2",
            "tags": [
                {
                    "name": "Microsoft"
                }
            ],
            "type": "manual",
        },
        {
            "id": "1",
            "tags": [
                {
                    "name": "Snow Leopard"
                }
            ],
            "type": "faq"
        },
        {
            "id": "2",
            "tags": [
                {
                    "name": "Microsoft"
                }
            ],
           "type": "manual"
        }
    ],
}

I tried using $login_data1['items'] = array_unique($login_data1['items'] ,SORT_REGULAR); but this adds serial numbers at the each json response

3
  • $input = array_map("unserialize", array_unique(array_map("serialize", $input))); try this by putting your variables Commented Jul 3, 2015 at 13:37
  • can you show your array format after unserialize it? Commented Jul 3, 2015 at 13:43
  • 1
    You can try array_unique($input,SORT_REGULAR) Commented Jul 3, 2015 at 13:45

4 Answers 4

2

Try as using array_unique

$json = '{
"items": [
    {
        "id": "2",
        "tags": [
            {
                "name": "Microsoft"
            }
        ],
        "type": "manual"
    },
    {
        "id": "1",
        "tags": [
            {
                "name": "Snow Leopard"
            }
        ],
        "type": "faq"
    },
    {
        "id": "2",
        "tags": [
            {
                "name": "Microsoft"
            }
        ],
       "type": "manual"
    }
]
}';

foreach(json_decode($json, true) as $key => $value){
    $input = array_unique($value,SORT_REGULAR);
}

If its an array then simply use

array_unique($login_data['items'],SORT_REGULAR);

Fiddle

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

2 Comments

when I try $json = $login_data1['items']; and your code, I am getting "Warning: json_decode() expects parameter 1 to be string, array given"
What is $login_data1['items'] is it an array or string
0

array_unique works perfectly if you pass a multidimensional array.

$login_data1['items'] = array_unique($login_data1['items'], SORT_REGULAR);

It doesn't work with your json because it's an array of object. Infact:

  $array = json_decode($json);
  var_dump($array);

returns:

    object(stdClass)#1 (1) {
  ["items"]=>
  array(3) {
    [0]=>
    object(stdClass)#2 (3) {
      ["id"]=>
      string(1) "2"
      ["tags"]=>
      array(1) {
        [0]=>
        object(stdClass)#3 (1) {
          ["name"]=>
          string(9) "Microsoft"
        }
      }
      ["type"]=>
      string(6) "manual"
    }
    [1]=>
    object(stdClass)#4 (3) {
      ["id"]=>
      string(1) "1"
      ["tags"]=>
      array(1) {
        [0]=>
        object(stdClass)#5 (1) {
          ["name"]=>
          string(12) "Snow Leopard"
        }
      }
      ["type"]=>
      string(3) "faq"
    }
    [2]=>
    object(stdClass)#6 (3) {
      ["id"]=>
      string(1) "2"
      ["tags"]=>
      array(1) {
        [0]=>
        object(stdClass)#7 (1) {
          ["name"]=>
          string(9) "Microsoft"
        }
      }
      ["type"]=>
      string(6) "manual"
    }
  }
}

Your json should look like this:

{
"items": [
    {
        "id": "2",
        "tags": {
            "name": "Microsoft"
        },
        "type": "manual"
    },
    {
        "id": "1",
        "tags": {
            "name": "Snow Leopard"
        },
        "type": "faq"
    },
    {
        "id": "2",
        "tags": {
            "name": "Microsoft"
        },
        "type": "manual"
    },
    {
        "id": "2",
        "tags": {
            "name": "Microsoft"
        },
        "type": "manual"
    }
]
}

And now:

  $array = json_decode($json);
  var_dump($array);

returns:

array(1) {
  ["items"]=>
  array(4) {
    [0]=>
    array(3) {
      ["id"]=>
      string(1) "2"
      ["tags"]=>
      array(1) {
        ["name"]=>
        string(9) "Microsoft"
      }
      ["type"]=>
      string(6) "manual"
    }
    [1]=>
    array(3) {
      ["id"]=>
      string(1) "1"
      ["tags"]=>
      array(1) {
        ["name"]=>
        string(12) "Snow Leopard"
      }
      ["type"]=>
      string(3) "faq"
    }
    [2]=>
    array(3) {
      ["id"]=>
      string(1) "2"
      ["tags"]=>
      array(1) {
        ["name"]=>
        string(9) "Microsoft"
      }
      ["type"]=>
      string(6) "manual"
    }
    [3]=>
    array(3) {
      ["id"]=>
      string(1) "2"
      ["tags"]=>
      array(1) {
        ["name"]=>
        string(9) "Microsoft"
      }
      ["type"]=>
      string(6) "manual"
    }
  }
}

And array_unique works.

1 Comment

array_unique helps remove duplicating array but it also adds serial number! how to remove serial number from each entry?
0

I got the solution for this.

$login_data1['items'] =array_values(array_unique($login_data1['items'] ,SORT_REGULAR));

Try array_value and array_unique together and serial number will be removed!

Comments

0
$login_data1['items'] =array_values(array_unique($login_data1['items'] ,SORT_REGULAR));

1 Comment

Thanks a lot! it helped me getting perfect result :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.