json_encode数据到JSON
json_decode JSON到PHP变量的转换
这两个PHP JSON互转函数在使用Ajax诸如:jquery和Ext框架的时候显得尤为重要,Ext默认接受的数据就是JSON格式,而jquery如果让JSON接入之后就显得更加轻松和简洁。本文将用一个实例讨论一下PHP 的JSON数据函数在jquery中的应用。
Jquery做为一个轻量级的Ajax框架确实显得短小而精悍,比较适合Web编程的前台页面,更加适合和PHP结合。其中jquery中有一个方法是:$.getJSON,这个用来从服务器脚本页面取得JSON格式的数据的。我们按顺序来,首先建立一个名为json.php的PHP脚本,具体程序如下:
<ol class="dp-xml"> <li class="alt"><span><span> </span><span class="tag"><span> ?php </span></span></span></li> <li><span>//创建一个联合数组,这里我们尽量真<br>实的模拟了一个从数据库中读取的记录 </span></li> <li class="alt"> <span>$</span><span class="attribute">a</span><span>=</span><span class="attribute-value">array</span><span>('name'=</span><span class="tag">></span><span>'dog','sex'=</span><span class="tag">><br></span><span>'f','par'=</span><span class="tag">></span><span>array('li','wang','lian')); </span> </li> <li> <span>$</span><span class="attribute">b</span><span>=</span><span class="attribute-value">array</span><span>('name'=</span><span class="tag">></span><span>'pig','sex'=</span><span class="tag">><br></span><span>'m','par'=</span><span class="tag">></span><span>array('liv','wange','liang')); </span> </li> <li class="alt"> <span>$</span><span class="attribute">array</span><span class="attribute-value">array</span><span> = array(</span><span class="attribute">0</span><span>=</span><span class="tag">></span><span>$a,</span><span class="attribute">1</span><span>=</span><span class="tag">></span><span>$b); </span> </li> <li><span>//将数组转化为一个json数据串 </span></li> <li class="alt"><span>echo json_encode($array); </span></li> <li> <span class="tag">?></span><span> </span> </li> </ol>
前台页面jquery代码:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> </span><span class="tag-name">script</span><span class="tag">></span><span> </span></span></span></li> <li><span>$(document).ready(function(){ </span></li> <li class="alt"><span>$.getJSON( </span></li> <li><span>'json.php', </span></li> <li class="alt"><span>function(data){ </span></li> <li> <span>var </span><span class="attribute">content</span><span>=</span><span class="attribute-value">""</span><span>; </span> </li> <li class="alt"> <span>for(var </span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>;i</span><span class="tag"><span class="tag-name">data.length</span><span>;i++)<br>{//循环读取后台的取得的JSON数据 </span></span> </li> <li><span>content+=data[i].name; </span></li> <li class="alt"> <span>var </span><span class="attribute">par</span><span>=</span><span class="attribute-value">"排挡:"</span><span>; </span> </li> <li> <span>for(var </span><span class="attribute">j</span><span>=</span><span class="attribute-value">0</span><span>;j</span><span class="tag"><span class="tag-name">data</span><span>[i].par.<br>length;j++){//循环读取数据中的par集合 </span></span> </li> <li class="alt"><span>par+=data[i].par[j]+"、"; </span></li> <li><span>} </span></li> <li class="alt"> <span>content+=par+"</span><span class="tag"><span class="tag-name">br</span><span class="tag">/></span><span>"; </span></span> </li> <li><span>} </span></li> <li class="alt"><span>alert(content); </span></li> <li><span>} </span></li> <li class="alt"><span>); </span></li> <li><span>}); </span></li> <li class="alt"><span class="tag"><span> /script</span><span class="tag">></span><span> </span></span></li> </ol>
本例在循环读取数据的时候应用了js的基本流程控制for循环方法,在jquery中也提供了一个遍历方法each有兴趣的兄弟们可以自己了解一下这个方法,底层实现也是基于for循环的。
注意:json_encode json_decode两个PHP JSON互转函数是在PHP5.2之后才有的,早于此前的版本使用将会报一个找不到函数的错误!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
