- A numerical array is converted to json for a REST API.
- The array is missing some indexes, maybe because we removed some items from it.
- json_encode, and thus Laravel, makes it an object, when we want an array.
// PHP input
json_encode([0 => "foo", 2 => "bar"]);
// Wa we get
{"0": "foo", "2": "bar"}
// What we want
["foo", "bar"]
We must re-index the array. For more complex data, we must do so recursively. We use laravel custom casts to make it easier and global.
Just add the reindexArrayRecusrive()
helper somewhere and call json_encode(reindexArrayRecusrive($value));