Json_encode after array_unique returns an associative array

$a = array("pear","apple","apple","ball","cat"); $u = array_unique($a); echo json_encode($u); 

The output looks like: {"0": "pear", "1": "apple", "3": "ball", "4": "cat"}

I need a non-associative array as a result: ["apple", "ball", "cat", "pear"].

+4
source share
1 answer

Flip the array with array_values before encoding it:

 echo json_encode(array_values($u)); 
+11
source

Source: https://habr.com/ru/post/1493606/


All Articles