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 Overflowphp - json_encode function: special characters - Stack Overflow
json_encode sometimes does or does not add keys for array elements - PHP - SitePoint Forums | Web Development & Design Community
Json_encode adds backslashes to a PHP object even when JSON_UNESCAPED_SLASHES is used
[PHP / JavaScript] How can I avoid removing square brackets from arrays in PHP when using json_encode
Videos
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]]
json_encode() function will help you to encode array to JSON in php.
if you will use just json_encode function directly without any specific option, it will return an array. Like mention above question
$array = array(
2 => array("Afghanistan",32,13),
4 => array("Albania",32,12)
);
$out = array_values($array);
json_encode($out);
// [["Afghanistan",32,13],["Albania",32,12]]
Since you are trying to convert Array to JSON, Then I would suggest to use JSON_FORCE_OBJECT as additional option(parameters) in json_encode, Like below
<?php
$array=['apple','orange','banana','strawberry'];
echo json_encode($array, JSON_FORCE_OBJECT);
// {"0":"apple","1":"orange","2":"banana","3":"strawberry"}
?>
For some reason your PHP was compiled without JSON. Either:
- Recompile it
- Install a package
- Use a function that emulates json_encode, which can be found in the comments section of http://php.net/manual/en/function.json-encode.php
If you're on Debian/Ubuntu, try:
apt-get install php5-json
and then
service php5-fpm restart
This will install the extension (this works on PHP 5.5.3).
The manual for json_encode specifies this:
All string data must be UTF-8 encoded.
Thus, try array_mapping utf8_encode() to your array before you encode it:
Copy$arr = array_map('utf8_encode', $arr);
$json = json_encode($arr);
// {"funds":"ComStage STOXX\u00c2\u00aeEurope 600 Techn NR ETF"}
For reference, take a look at the differences between the three examples on this fiddle. The first doesn't use character encoding, the second uses htmlentities and the third uses utf8_encode - they all return different results.
For consistency, you should use utf8_encode().
Docs
- json_encode()
- utf8_encode()
- array_map()
Your input has to be encoded as UTF-8 or ISO-8859-1.
http://www.php.net/manual/en/function.json-encode.php
Because if you try to convert an array of non-utf8 characters you'll be getting 0 as return value.
Since 5.5.0 The return value on failure was changed from null string to FALSE.