I have an array below in which I want to get or remove duplicate values:
$data = $responses;
var_dump($data);
result is:
array(11) {
[0]=>
string(4) "date"
[1]=>
string(4) "name"
[2]=>
string(10) "start_time"
[3]=>
string(8) "end_time"
[4]=>
string(4) "date"
[5]=>
string(4) "name"
[6]=>
string(10) "start_time"
[7]=>
string(8) "end_time"
[8]=>
string(7) "other_1"
[9]=>
string(7) "other_2"
[10]=>
string(7) "other_3"
}
So I do :
var_dump(array_unique($data));
//also tried:
var_dump(array_intersect_key($data, array_unique(array_map('serialize', $data))));
but the output is:
array(6) {
[0]=>
string(4) "date"
[1]=>
string(4) "name"
[2]=>
string(10) "start_time"
[3]=>
string(8) "end_time"
[4]=>
string(7) "other_1"
[5]=>
string(7) "other_2"
[6]=>
string(7) "other_3"
}
The output I am expecting is:
array(3){
[1]=>
string(7) "other_1"
[2]=>
string(7) "other_2"
[3]=>
string(7) "other_3"
}
I am expecting to have the last 3 items to be returned only for they are the only values that are not duplicate, but I think array_unique
is just retaining one single value of the duplicate items.
Am I doing array_unique
the right way?