Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Array to JSON Array using json_encode(); [duplicate]

Tags:

json

arrays

php

I've encoded an Array I've made using the inbuilt json_encode(); function. I need it in the format of an Array of Arrays like so:

[["Afghanistan",32,12],["Albania",32,12]] 

However, it is returning as:

{"2":["Afghanistan",32,12],"4":["Albania",32,12]} 

How can I remove these row numbers without using any Regex trickery?

like image 767
Ryan Brodie Avatar asked Jul 30 '12 12:07

Ryan Brodie


People also ask

What does the PHP function json_encode () do?

PHP | json_encode() Function The json_encode() function is an inbuilt function in PHP which is used to convert PHP array or object into JSON representation. Parameters: $value: It is a mandatory parameter which defines the value to be encoded.

What is use of json_encode?

The json_encode() function is used to encode a value to JSON format.

What is the difference between json_encode and json_decode?

PHP json_decode() and json_encode(): Summary JSON can be handled by using inbuilt PHP functions. For example, a PHP object might be turned into JSON format file using PHP json_encode() function. For the opposite transformation, use PHP json_decode() . PHP arrays are not supported by XML but can be converted into JSON.

Which function in PHP can be used to convert objects in PHP into JSON?

json_encode() is a native PHP function that allows you to convert PHP data into the JSON format. The function takes in a PHP object ($value) and returns a JSON string (or False if the operation fails).

How to convert an array to JSON in PHP?

To convert an array to json in PHP, use the json_encode () function. The json_encode () function is used to encode a value to JSON format. The json_encode () function converts PHP-supported data type into JSON formatted string to be returned due to JSON encode operation. See the following code. See the output.

What is JSON_Encode () function in PHP?

The json_encode () function is used to convert the value of the array into JSON. This function is added in from PHP5. Also, you can make more nesting of arrays as per your requirement. You can also create an array of array of objects with this function.

How to convert associative array to JSON string using JSON_Encode() function?

json_encode () returns a string, or false if the encoding is not successful. In this example, we take an associative array and convert it into a JSON string using json_encode () function with the default optional parameters. We shall display the string returned by json_encode () in the output.

What happens if JSON_last_error () fails to encode an array?

In the event of a failure to encode, json_last_error () can be used to determine the exact nature of the error. When encoding an array, if the keys are not a continuous numeric sequence starting from 0, all keys are encoded as strings, and specified explicitly for each key-value pair.


1 Answers

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]] 
like image 114
Michael Berkowski Avatar answered Sep 23 '22 23:09

Michael Berkowski