0

Hi this is my var_dump result. My problem is : If my array contains more than once the same "name"(in my case "Test2"), then remove one of them. In my case all of the information from [0] or [1]. Thx.

array(3) {
  [0]=>
  object(stdClass)#24 (14) {
    ["name"]=>
    string(19) "Test2"
    ["taskid"]=>
    string(5) "11526"
  }
  [1]=>
  object(stdClass)#25 (14) {
    ["name"]=>
    string(19) "Test2"
    ["taskid"]=>
    string(5) "11526"
  }
  [2]=>
  object(stdClass)#26 (14) {
    ["name"]=>
    string(19) "Test1"
    ["taskid"]=>
    string(5) "11525"
  }
}

I want this result :

array(3) {
      [0]=>
      object(stdClass)#24 (14) {
        ["name"]=>
        string(19) "Test2"
        ["taskid"]=>
        string(5) "11526"
      }
      [1]=>
      object(stdClass)#26 (14) {
        ["name"]=>
        string(19) "Test1"
        ["taskid"]=>
        string(5) "11525"
      }
    }

2 Answers 2

1

In you example dublicates not an arrays but objects, so you need to combine like this array_map and array_unique:

$arrayResult = array_map("unserialize", array_unique(array_map("serialize", $inputArray)));

Or try

$arrayResult = array();
foreach ( $inputArray as $item ) {
    isset($arrayResult[$item->taskid]) or $arrayResult[$item->taskid] = $item;
}

var_dump($arrayResult); 
Sign up to request clarification or add additional context in comments.

3 Comments

you are right about objects, but your example gives the same result.
now it works. Can you explain me what did you done ? :D
You set an object into new array, but before you check if it exist in new array, if no it will be added, else it will not
0

You can use array_unique() function

Comments

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.