Home > Backend Development > PHP Problem > php convert array to json format

php convert array to json format

藏色散人
Release: 2023-03-11 14:12:01
Original
3100 people have browsed it

php method to convert an array to json format: first create a PHP sample file; then pass "$arr = [10,'Tom',true, '2015-10-15'];echo json_encode( $arr);" statement can be converted.

php convert array to json format

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How does php convert an array into json format? ?

PHP converts an array into a JSON string

<?php
/**PHP把数组转换为JSON字符串**/
$arr = [10,&#39;Tom&#39;,true, &#39;2015-10-15&#39;];
//echo $arr;
echo json_encode($arr);//编码为JSON字符串
$arr = [&#39;eid&#39;=>10,&#39;ename&#39;=>&#39;Tom&#39;,&#39;isMarried&#39;=>true, &#39;birthday&#39;=>&#39;2015-10-15&#39;];
//echo $arr;
echo json_encode($arr);//编码为JSON字符串
//结论:索引数组会被json_encode转换为JSON数组,关联数组会被json_encode转换为JSON对象
 ?>
Copy after login

The results are as follows

[10,"Tom",true,"2015-10-15"]
{"eid":10,"ename":"Tom","isMarried":true,"birthday":"2015-10-15"}
Copy after login

Recommended learning: "PHP Video Tutorial"

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

Related labels:
php
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template