2

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Malibur
  • 1,695
  • 5
  • 22
  • 31
  • possible duplicate of [How to remove duplicate values from a multi-dimensional array in PHP](http://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php) – Damodaran Nov 21 '13 at 12:56

1 Answers1

3

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.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • I love stack overflow. - Thank you for a quick response. Ill head over to the manual to see what that flag means. Ill accept this answer as soon as im allowed (in 5 mins). – Malibur Nov 21 '13 at 13:01
  • i dont understand the difference between sort_regular, and sort_string? is it because i have integers and strings in my case, that sort_string wouldn't work? – Malibur Nov 21 '13 at 15:31
  • @MaltheMilthers: By default `array_unique()` first sorts the values inside the array as strings, loops through them and then remove the duplicates. When `SORT_REGULAR` is used, the data type of array element will be taken as it is for sorting without any modification. In other words, `array_unique()` will be doing the same as an equality check with `==`. – Amal Murali Nov 21 '13 at 15:44