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?
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.
The json_encode() function is used to encode a value to JSON format.
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.
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).
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.
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.
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.
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.
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:
// 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]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With