If the array keys in your PHP array are not consecutive numbers, json_encode() must make the other construct an object since JavaScript arrays are always consecutively numerically indexed.

Use array_values() on the outer structure in PHP to discard the original array keys and replace them with zero-based consecutive numbering:

Example:

// Non-consecutive 3number keys are OK for PHP
// but not for a JavaScript array
$array = array(
  2 => array("Afghanistan", 32, 13),
  4 => array("Albania", 32, 12)
);

// array_values() removes the original keys and replaces
// with plain consecutive numbers
$out = array_values($array);
json_encode($out);
// [["Afghanistan", 32, 13], ["Albania", 32, 12]]
Answer from Michael Berkowski on Stack Overflow
🌐
PHP
php.net › manual › en › function.json-encode.php
PHP: json_encode - Manual
For example, the string "Example\" will cause the following error in JavaScript when parsing: JSON.parse: expected ',' or '}' after property value in object This appears to be an oversight in the JSON specification and the only work-around appears to be removing trailing slashes from all strings before encoding. ... Be careful with floating values in some locales (e.g. russian) with comma (",") as decimal point. Code: <?php setlocale(LC_ALL, 'ru_RU.utf8'); $arr = array('element' => 12.34); echo json_encode( $arr ); ?> Output will be: -------------- {"element":12,34} -------------- Which is NOT a valid JSON markup.
🌐
W3Schools
w3schools.com › php › func_json_encode.asp
PHP json_encode() Function
zip_close() zip_entry_close() ... zip_entry_read() zip_open() zip_read() PHP Timezones ... The json_encode() function is used to encode a value to JSON format....
Discussions

php - json_encode function: special characters - Stack Overflow
Elements of an array containing special characters are converted to empty strings when encoding the array with json_encode: $arr = array ( "funds" => "ComStage STOXX®Europe 600 Te... More on stackoverflow.com
🌐 stackoverflow.com
json_encode sometimes does or does not add keys for array elements - PHP - SitePoint Forums | Web Development & Design Community
I discovered the other day that I json_encode was converting php arrays into json and sometimes, but sometimes not, adding indexes to elements. $array1 = array( 'first', array('second'=> 'third', 'fourth' => 'f… More on sitepoint.com
🌐 sitepoint.com
0
March 22, 2015
Json_encode adds backslashes to a PHP object even when JSON_UNESCAPED_SLASHES is used
I think you are double encoding. You have JSON is string, then json encode that string. I think what you want is one PHP data structure (probably an array) with all the data then simply encode that php data into JSON. Try somthing like this; [ 'data' => [ 'apiRequest' => [ 'signature' => 'I3d4b8680011fa544e9d5dab9f3', ], ], ], 'status' => 'success', ]; var_dump($message); $json = json_encode($message, JSON_UNESCAPED_SLASHES); var_dump($json); More on reddit.com
🌐 r/PHPhelp
5
1
November 30, 2021
[PHP / JavaScript] How can I avoid removing square brackets from arrays in PHP when using json_encode
So I have an array of objects that I converted to JSON using JSON.stringify in JavaScript You turn the array into a string representation of JSON data, there's no reason to call json_encode on that string, json_encode is for calling on php arrays and objects. More on reddit.com
🌐 r/learnprogramming
4
1
January 28, 2014
🌐
Scout APM
scoutapm.com › blog › php-json-encode-serialize-php-objects-to-json
PHP Json_encode: Serialize PHP Objects to JSON
Let’s see how we can convert our PHP variables into JSON format. json_encode() is a native PHP function that allows you to convert PHP data into the JSON format.
🌐
OnlinePHP
onlinephp.io › json-encode
json_encode - Online Tool
PHP Functions · General · json_encode · Execute json_encode with this online tool json_encode() - Returns the JSON representation of a value · Json Encode Online Tool · Manual · Code Examples · Json Encode Online Tool · Manual · Code ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-json_encode-function
PHP | json_encode() Function - GeeksforGeeks
June 17, 2019 - The json_encode() function is an inbuilt function in PHP which is used to convert PHP array or object into JSON representation.
🌐
ReqBin
reqbin.com › code › php › 4qfsaffq › php-json-encode-example
How do I encode a PHP object to JSON string?
July 5, 2023 - To encode a PHP object to a JSON formatted string, you can use the json_encode($value, $options, $depth) function. The $value parameter specifies the PHP object to encode. The additional $options parameter provides additional options for JSON ...
🌐
W3Schools
w3schools.com › js › js_json_php.asp
JSON PHP
Objects in PHP can be converted into JSON by using the PHP function json_encode():
🌐
W3Schools
w3schools.com › php › php_json.asp
PHP and JSON
PHP has some built-in functions to handle JSON. First, we will look at the following two functions: ... The json_encode() function is used to encode a value to JSON format.
🌐
Code Beautify
codebeautify.org › json-encode-online
JSON Encode Online to encode JSON from stdClass Object of PHP to readable form.
JSON Encode Online is easy to use tool to encode JSON data, which converts stdClass Object of PHP to JSON.
🌐
SitePoint
sitepoint.com › php
json_encode sometimes does or does not add keys for array elements - PHP - SitePoint Forums | Web Development & Design Community
TL:DR; use json_encode(array_values($array2)) to produce JSON arrays [indexed]. ... I always have a hard time explaining why your example behaves like that, so here’s the overly wordy version: PHP doesn’t distinguish between indexed and associative array, all arrays are associative and that’s that [ref PHP doc]. PHP’s JSON encoder does distinguish the two.
Published   March 22, 2015
🌐
Jobtensor
jobtensor.com › Tutorial › PHP › en › JSON
PHP JSON - json_encode(), json_decode() | jobtensor
<?php $countries = array("Mark" => "USA", "Raymond" => "UK", "Jeff" => "JPN", "Mike" => "DE"); echo json_encode($countries); The json_decode() function is used to decode a JSON object into a PHP associative array or object. It has an optional second parameter that gives a PHP associative array instead of the default PHP object.
🌐
Carmatec
carmatec.com › home › understanding php json_encode() and json_decode()
Understanding PHP json_encode() and json_decode() - Carmatec
October 14, 2025 - The json_encode() function in PHP converts a PHP variable (such as an array or object) into a JSON-formatted string. Conversely, json_decode() takes a JSON string and converts it back into a PHP variable, typically an array or object.