Malibur
Malibur

Reputation: 1783

php multidimensional array: how to remove duplicate entries

in fear of duplicating content, i have looked through so many similar SO questions, but i think i need a bit more than code, to tell me how solve my problem- would be lovely with some explaination too.

How do i turn $list:

array(4) {
  [0]=>
  array(2) {
    ["title"]=>
    string(8) "Zambezia"
    ["id"]=>
    int(31)
  }
  [1]=>
  array(2) {
    ["title"]=>
    string(6) "Zarafa"
    ["id"]=>
    int(34)
  }
  [2]=>
  array(2) {
    ["title"]=>
    string(8) "Zambezia"
    ["id"]=>
    int(31)
  }
  [3]=>
  array(2) {
    ["title"]=>
    string(8) "Zambezia"
    ["id"]=>
    int(31)
  }
} 

Into $list:

 array(2) {
      [0]=>
      array(2) {
        ["title"]=>
        string(8) "Zambezia"
        ["id"]=>
        int(31)
      }
      [1]=>
      array(2) {
        ["title"]=>
        string(6) "Zarafa"
        ["id"]=>
        int(34)
      }
    } 

By removing duplicate entries?

Upvotes: 2

Views: 111

Answers (1)

Amal Murali
Amal Murali

Reputation: 76656

Use array_unique() with SORT_REGULAR flag.

$new_array = array_unique($array, SORT_REGULAR);

Output should be:

Array
(
    [0] => Array
        (
            [title] => Zambezia
            [id] => 31
        )

    [1] => Array
        (
            [title] => Zarafa
            [id] => 34
        )

)

Demo.

Upvotes: 3

Related Questions