user3886556
user3886556

Reputation: 239

remove duplicate values from multidimensional array

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

Upvotes: 2

Views: 91

Answers (4)

user3752006
user3752006

Reputation: 96

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

Upvotes: 0

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21437

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

Upvotes: 2

user3886556
user3886556

Reputation: 239

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!

Upvotes: 0

FedericoFiorini
FedericoFiorini

Reputation: 44

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.

Upvotes: 0

Related Questions