Home > Backend Development > PHP Problem > How to convert php array into json string

How to convert php array into json string

PHPz
Release: 2023-04-26 13:40:29
Original
577 people have browsed it

PHP is a programming language widely used for web development, while JSON (JavaScript Object Notation) is a lightweight data exchange format. In web development, it is often necessary to convert arrays in PHP into JSON format strings to facilitate data exchange between web applications.

In PHP, you can use the built-in json_encode function to convert an array into a JSON format string. The following is a simple sample code:

<?php
  $arr = array(&#39;name&#39; => 'John', 'age' => 30, 'city' => 'New York');
  $json = json_encode($arr);
  echo $json;
?>
Copy after login

The above code first defines an associative array named $arr, which contains three elements: name, age and city. Then, pass the array to the json_encode function to convert it into a JSON-formatted string, and assign the result to the $json variable. Finally, use the echo function to output the JSON string to the screen.

When the above code is executed, the following content will be displayed on the screen:

{"name":"John","age":30,"city":"New York"}
Copy after login

You can see that the json_encode function successfully $arr The array is converted into a string that conforms to the JSON specification.

In addition to associative arrays, ordinary arrays in PHP can also be converted into JSON format strings. The following is a sample code that converts an ordinary array into a JSON string:

<?php
  $arr = array(&#39;John&#39;, &#39;Alice&#39;, &#39;Bob&#39;);
  $json = json_encode($arr);
  echo $json;
?>
Copy after login

The above code defines an ordinary array $arr, which contains three string elements. Then use the json_encode function to convert it into a JSON format string, and use the echo function to output the result to the screen.

After executing the above code, the following content will be displayed on the screen:

["John","Alice","Bob"]
Copy after login

This result is slightly different from the previous conversion result of the associative array, mainly because ordinary arrays cannot be used in JSON format Displayed in the form of key-value pairs, it will be automatically converted into a simple string array.

In addition to basic array conversion, the json_encode function also has many optional parameters and options that can be used to further customize the output format and encoding. For example, you can use the JSON_UNESCAPED_UNICODE option of the json_encode function to ensure that all Unicode characters are correctly encoded. The following is a sample code:

<?php
  $arr = array(&#39;name&#39; => '张三', '城市' => '北京');
  $json = json_encode($arr, JSON_UNESCAPED_UNICODE);
  echo $json;
?>
Copy after login

The above code defines an associative array $arr, which contains two elements, one is the key name containing Chinese characters name, the other is the key name city containing Chinese characters. Then, pass that array to the json_encode function, using the JSON_UNESCAPED_UNICODE option to ensure that the Unicode characters are all encoded correctly. Finally, use the echo function to output the results to the screen.

After executing the above code, the following content will be displayed on the screen:

{"name":"张三","城市":"北京"}
Copy after login

In short, converting an array into a string in JSON format is a common task in web development. In PHP, you can easily implement this functionality using the json_encode function, with optional parameters and options for further customization.

The above is the detailed content of How to convert php array into json string. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template