I would like to remove duplicates for the following array. I want to group by the first value and then rebuild the index.
ie
Array
(
[0] => Array
(
[title] => California
[state_id] => 1
)
[1] => Array
(
[title] => California
[state_id] => 1
)
[2] => Array
(
[title] => New Mexico
[state_id] => 2
)
[3] => Array
(
[title] => Washington
[state_id] => 3
)
[4] => Array
(
[title] => Montana
[state_id] => 4
)
[5] => Array
(
[title] => Montana
[state_id] => 4
)
)
To
Array
(
[0] => Array
(
[title] => California
[state_id] => 1
)
[2] => Array
(
[title] => New Mexico
[state_id] => 2
)
[3] => Array
(
[title] => Washington
[state_id] => 3
)
[4] => Array
(
[title] => Montana
[state_id] => 4
)
)
and rebuild key
Array
(
[0] => Array
(
[title] => California
[state_id] => 1
)
[1] => Array
(
[title] => New Mexico
[state_id] => 2
)
[2] => Array
(
[title] => Washington
[state_id] => 3
)
[3] => Array
(
[title] => Montana
[state_id] => 4
)
)
I.e.
titlekey (array_map)array_combine), which de-duplicates the array (keys have to be unique)array_values, which discards the keysBroken down:
For PHP 5.2- you’ll need to write the anonymous callback function like this: