Answer the question
In order to leave comments, you need to log in
JsonResponse strip array indexes from result if they start from zero?
Tell me, there is an array:
Array
(
[List] => Array
(
[0] => Array
(
[guid] => Пустое значение поля глобальный идентификатор региона
)
[1] => Array
(
[guid] => Пустое значение поля глобальный идентификатор региона
)
)
)
{
"List": [
{
"guid": "Пустое значение поля глобальный идентификатор региона"
},
{
"guid": "Пустое значение поля глобальный идентификатор региона"
}
]
}
"List": {
"1": {
"guid": "Пустое значение поля глобальный идентификатор региона"
}
}
Answer the question
In order to leave comments, you need to log in
JsonResponse has nothing to do with it. It's about json_encode. You need the JSON_FORCE_OBJECT option
use Symfony\Component\HttpFoundation\JsonResponse;
$data = [
'List' => [
0 => [
'id' => 'id0',
],
1 => [
'id' => 'id1',
],
],
];
dump(
new JsonResponse(
json_encode($data, JsonResponse::DEFAULT_ENCODING_OPTIONS | JSON_FORCE_OBJECT),
200,
[],
true
)
);
// or
$response = new JsonResponse($data);
$response->setEncodingOptions(JsonResponse::DEFAULT_ENCODING_OPTIONS | JSON_FORCE_OBJECT);
dump($response->getContent());
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question