PHP array to JSON object and keep numeric index

Source: Internet
Author: User
Tags php array to json

The problem with this article is that PHP's non-indexed arrays are converted to JSON objects (rather than the default JSON array)

The method used by the array to JSON in PHP is the system's own: Json_encode

The rules for array-to-json in PHP are: when the index is not specified, it is converted to a JSON array, and the index is converted to a JSON object.

So the question is, if you need to convert an array that doesn't have an index into a JSON object, let's look at both of these cases:

1. No index is specified:

$val = Array ("A", "B", "C", "D", "E");

Convert to JSON:

["A", "B", "C", "D", "E"]

2, there is an index of the situation:

$val = Array ("A" = "a", "B" = "B", "C" = "C", "D" = "D", "e" = "E");

Convert to JSON:

{    "a": "A", "    B": "B",    "C": "C",    "D": "D",    "E": "E"}

So let's go back to the first case if you want to convert to a JSON object as follows without an index:

{    "0": "A",    "1": "B", "    2": "C",    "3": "D",    "4": "E"}

Some people say that we can solve the problem by manually specifying an index with another array and converting it:

$arr = Array ();  $val = Array ("A", "B", "C", "D", "E");  for ($i =0; $i <count ($val), $i + +) {      $arr [". $i]= $val [$i];  }
echo Json_encode ($arr);

The reality is that this does not solve the problem, and the output is still a JSON array.

The reason is: The default index of the array in PHP is the number 0~n (depending on the length of the array), the index is not specified by default is the number sequence index, and manually specify the number sequence index and do not specify the index effect is the same . Therefore, even if the index is specified, it is useless.

The solution is also simple, as long as the PHP default index rules can be scrambled!

We only need to specify an index when the $arr array is initialized, and PHP will assume that the index of the array has been changed by the user and is no longer managed by the system.

The above code changes to:

 $arr = Array (1=>0);  $val = Array ("A", "B", "C", "D", "E");  for ($i =0; $i <count ($val), $i + +) {      $arr [". $i]= $val [$i];  } echo Json_encode ($arr);

Please note that the code in red, with this initialization, the code in the back loop can be executed according to our expected results.

The use of PHP arrays is far more than these tips and points, and we need to work hard to find out.

PS: Careful study of PHP and JS I only gradually found that the original weak type of language is a high-level language, and the strong type of language is a fool-style language.

PHP array to JSON object and keep numeric index

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.