$a = array("pear","apple","apple","ball","cat");
$u = array_unique($a);
echo json_encode($u);
The output appears as: {"0":"pear","1":"apple","3":"ball","4":"cat"}
I need a non-associative array as output as: ["apple","ball","cat","pear"].
Reindex the array with array_values before encoding it:
echo json_encode(array_values($u));
array_unique chooses to remove, which is AFAIK not deterministic from a PHP userland perspective. If it removes the second "apple" you don't need to reindex, but if it removes the first you do. You can't control this, so reindexing is necessary to be safe.
json_encode,http_build_query,htmlspecialcharsetc.