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?

1

1 Answer 1

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.

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

3 Comments

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).
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?
@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 ==.

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.