Search code examples
phparray-unique

php array_unique method "puts key into array"


Im going to parse json to my array by unique values. But here is some problem about array_unique function. For example:

$contract_types = [ "Asset Sale and Purchase Agreement", "Asset Sale and Purchase Agreement", "Concession Agreement" ];

and

return array_unique($contract_types);

gives me: [{ "0": "Asset Sale and Purchase Agreement", "2": "Concession Agreement" }]

What i'm doing wrong?


Solution

  • array_unique() preserves keys. From PHP docs:

    Note that keys are preserved.

    If you wish to reindex the array so it has consecutive integer indexes, use array_values():

    return array_values(array_unique($contract_types));