0

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?

Alok Patel
  • 7,842
  • 5
  • 31
  • 47

2 Answers2

0

You mean that your keys are 0, 2 and you wish you had 0,1 ?

If so, perform a array_values :

$contract_types = array_unique($contract_types);

return array_values($contract_types);
jiboulex
  • 2,963
  • 2
  • 18
  • 28
0

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));
Shira
  • 6,392
  • 2
  • 25
  • 27