php returns json

Jul 29, 2016 am 09:12 AM
gt json lt quot

<?php 
include './include/conn.php';  //数据库链接文件
$sql_notice = mysql_query('SELECT * FROM gg_notice where enable = "1" limit 0,10');
$notice = mysql_fetch_array($sql_notice, MYSQL_ASSOC);
print_r ($notice);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>第一php网提供的教程--将数据库读取的数据生成json格式</title>
<meta http-equiv="Content-Type" c/html; charset=utf-8" />
&lt;!-- &lt;script src=&quot;http://<strong>ajax</strong>.googleapis.com/<strong>ajax</strong>/libs/jquery/1.4/jquery.min.js&quot; type=&quot;text/javascript&quot;/&gt;&lt;/script&gt; --&gt;
&lt;script language=javascript&gt;
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;pre&gt;
&lt;h1&gt;请注意两种方法生成的对象数组在结构上的区别&lt;/h1&gt;
&lt;?php
echo '&lt;h1&gt;法一&lt;/h1&gt;';
//假设以下数组是根据我们从数据库读取的数据生成的
$jarr=array('total'=&gt;239,'row'=&gt;array(
           array('code'=&gt;'001','name'=&gt;'中国','addr'=&gt;'Address 11','col4'=&gt;'col4 data'),
           array('code'=&gt;'002','name'=&gt;'Name 2','addr'=&gt;'Address 12','col4'=&gt;'col4 data'),
                                     )
           );
//法一:
$jobj=new stdclass();//实例化stdclass,这是php内置的空类,可以用来传递数据,由于json_decode后的数据是以对象数组的形式存放的,
//所以我们生成的时候也要把数据存储在对象中
foreach($jarr as $key=&gt;$value){
$jobj-&gt;$key=$value;
}
print_r($jobj);//打印传递属性后的对象
echo '使用$jobj-&gt;row[0][\'code\']输出数组元素:'.$jobj-&gt;row[0]['code'].'&lt;br&gt;';
echo '编码后的<strong>json字符串</strong>:'.json_encode($jobj).'&lt;br&gt;';//打印编码后的<strong>json字符串</strong>




echo '&lt;hr&gt;';
//法二:
echo '&lt;h1&gt;法二&lt;/h1&gt;';
echo '编码后的<strong>json字符串</strong>:';
echo $str=json_encode($jarr);//将数组进行json编码
echo '&lt;br&gt;';
$arr=json_decode($str);//再进行json解码
print_r($arr);//打印解码后的数组,数据存储在对象数组中
echo '使用$arr-&gt;row[0]-&gt;code输出数组元素:'.$arr-&gt;row[0]-&gt;code;


?&gt; 


&lt;/body&gt;
&lt;/html&gt;
Copy after login

The above introduces how PHP returns json, including ajax and json string content. I hope it will be helpful to friends who are interested in PHP tutorials.

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

What are the differences between Huawei GT3 Pro and GT4?

Combination of golang WebSocket and JSON: realizing data transmission and parsing Combination of golang WebSocket and JSON: realizing data transmission and parsing Dec 17, 2023 pm 03:06 PM

Combination of golang WebSocket and JSON: realizing data transmission and parsing

What is the difference between MySQL5.7 and MySQL8.0? What is the difference between MySQL5.7 and MySQL8.0? Feb 19, 2024 am 11:21 AM

What is the difference between MySQL5.7 and MySQL8.0?

How to exclude a field from JSON using @Expose annotation in Java? How to exclude a field from JSON using @Expose annotation in Java? Sep 16, 2023 pm 09:49 PM

How to exclude a field from JSON using @Expose annotation in Java?

Performance optimization tips for converting PHP arrays to JSON Performance optimization tips for converting PHP arrays to JSON May 04, 2024 pm 06:15 PM

Performance optimization tips for converting PHP arrays to JSON

Use the json.MarshalIndent function in golang to convert the structure into a formatted JSON string Use the json.MarshalIndent function in golang to convert the structure into a formatted JSON string Nov 18, 2023 pm 01:59 PM

Use the json.MarshalIndent function in golang to convert the structure into a formatted JSON string

Pandas usage tutorial: Quick start for reading JSON files Pandas usage tutorial: Quick start for reading JSON files Jan 13, 2024 am 10:15 AM

Pandas usage tutorial: Quick start for reading JSON files

How to handle XML and JSON data formats in C# development How to handle XML and JSON data formats in C# development Oct 09, 2023 pm 06:15 PM

How to handle XML and JSON data formats in C# development

See all articles