Home  >  Article  >  Backend Development  >  php convert array to json format

php convert array to json format

藏色散人
藏色散人Original
2021-07-14 09:41:113012browse

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对象
 ?>

The results are as follows

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

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!

Statement:
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