Removing array index reference when using json_encode()
You are ignoring the return value of array_values
in your existing attempt to reindex the array. Correct is
$arr['dates'] = array_values($arr['dates']);
The reindexing should also be moved outside the foreach
loop, there is no point in reindexing multiple times.
Use array_values()
for your issue:
$arr['dates'] = array_values($arr['dates']);
//..
$arr = json_encode($arr);
Why? Because you're unsetting array's key without re-ordering it. So after this the only way to keep that in JSON will be encode keys too. After applying array_values()
, however, you'll get ordered keys (starting from 0
) which can be encoded properly without including keys.
In Laravel collections(just in case) you can do
$newArray = $collection->values()->toArray();
or
$jsonEncoded = $collection->values()->toJson();