PHP Array to JSON Array using json_encode();
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 PHP array as JSON array not JSON object
See Arrays in RFC 8259 The JavaScript Object Notation (JSON) Data Interchange Format:
An array structure is represented as square brackets surrounding zero
or more values (or elements). Elements are separated by commas.array = begin-array [ value *( value-separator value ) ] end-array
You are observing this behaviour because your array is not sequential - it has keys 0
and 2
, but doesn't have 1
as a key.
Just having numeric indexes isn't enough. json_encode
will only encode your PHP array as a JSON array if your PHP array is sequential - that is, if its keys are 0, 1, 2, 3, ...
You can reindex your array sequentially using the array_values
function to get the behaviour you want. For example, the code below works successfully in your use case:
echo json_encode(array_values($input)).
PHP array not converting to JSON using json_encode
If json_last_error gives you the JSON_ERROR_UTF8
error code (5), then you will have to encode your datas in UTF-8 :
Need to convert final array to utf-8.
<?php
$response = array_map('utf8_encode', $response);
getting json object instead of json array in php
You need to do it like this:-
$response = array();
//$response['images'] = array(); not needed
//traversing through all the rows
while($row = mysqli_fetch_assoc($result)){ //since you are using name indexes so use _assoc()
$temp = array();
$temp['url']=$row['url'];
$temp['name']=$row['name'];
$response[] =$temp;
}
//displaying the response
echo json_encode($response);
Encode PHP array with non-numeric keys to JSON array
You can use array_values
:
echo json_encode(array_values($a));
PHP/json_encode: dealing with mixed arrays and objects with numeric properties
Was able to find a solution by using stdClass
instead of an associative array when decoding the original JSON via json_decode($json, false);
. Then when json_encode
ing the resulting stdClass
the keys will be preserved.
Here's a full example:
<?php
$json = <<<JSON
{
"someList": [
"item A",
"item B"
],
"ratings": {
"0": 0.001234,
"1": 0.0666,
"2": 0.09876,
"3": 0.777777
}
}
JSON;
// Passing false for the second param (or omitting it)
// returns a stdClass instead of associative array
$data = json_decode($json, false);
echo json_encode($data, JSON_PRETTY_PRINT);
Which outputs:
{
"someList": [
"item A",
"item B"
],
"ratings": {
"0": 0.001234,
"1": 0.0666,
"2": 0.09876,
"3": 0.777777
}
}
Encode array to JSON string without array indexes
You want PHP's array_values()
function:
$json_out = json_encode(array_values($your_array_here));
Related Topics
How to Execute Two MySQL Queries as One in PHP/MySQL
Login to Remote Site With PHP Curl
How to to Send Mail Using Gmail in Laravel
MySQLi::MySQLi(): (Hy000/2002): Can't Connect to Local MySQL Server Through Socket 'MySQL' (2)
PHP Simplexml How to Save the File in a Formatted Way
List All the Files and Folders in a Directory With PHP Recursive Function
Using Sessions & Session Variables in a PHP Login Script
Explode PHP String by New Line
How to Get JavaScript Variable Value in PHP
Index.PHP Not Loading by Default
How to Set HTML Value Attribute (With Spaces)
How to Refresh Token With Google API Client
PHP Reading Shell_Exec Live Output
PHP - Insert a Variable in an Echo String
Best Solution to Protect PHP Code Without Encryption