可以用$obj=key 的方式直接讀取 ?也可以先轉(zhuǎn)換為數(shù)組 ?用遍歷數(shù)組的方法讀取
成都創(chuàng)新互聯(lián)公司主要從事成都做網(wǎng)站、成都網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)鐘山,十載網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220
?php
$json?=?'{"a":100,"b":200,"c":300,"d":400,"e":500}';
//首先將json字符串轉(zhuǎn)換成關(guān)聯(lián)數(shù)組
$arr=json_decode($json,?true);?
//然后循環(huán)讀取數(shù)據(jù)
foreach($arr?as?$item){
echo?$item;
echo?"br/";
}
?
運(yùn)行結(jié)果:
100
200
300
400
500
PHP直接的函數(shù)獲取或生成
用php生成json格式:json_encode('內(nèi)容');
用php讀取json數(shù)據(jù):json_deconde('json數(shù)據(jù)');
$dataJson = "數(shù)據(jù)"; // 提供數(shù)據(jù)
$data = json_decode($dataJson); // 數(shù)據(jù)按json解析成php數(shù)組
$minuteArr = $data['minute']; // 從數(shù)組中獲取minute數(shù)據(jù)
此時$minuteArr中存放的就是你要的數(shù)據(jù),但數(shù)組下標(biāo)(鍵)是從0開始,如需要從1開始,則遍歷$minuteArr修改下標(biāo)(鍵) 或 使用php數(shù)組函數(shù)進(jìn)行快速重構(gòu)。
注:你這問題是還沒入門的初學(xué)者問的問題,建議還是多看看書!...
// 生成一個PHP數(shù)組
$data = array();
$data['name'] = 'admin';
$data['pwd'] = '123456';
// 把PHP數(shù)組轉(zhuǎn)成JSON字符串
$json_string = json_encode($data);
// 寫入文件
file_put_contents('user.json', $json_string);
// 從文件中讀取數(shù)據(jù)到PHP變量
$json_string = file_get_contents('user.json');
// 把JSON字符串轉(zhuǎn)成PHP數(shù)組
$data = json_decode($json_string, true);
// 顯示出來看看
var_dump($data)
當(dāng)前標(biāo)題:php獲取json數(shù)據(jù)名,php返回json數(shù)據(jù)
URL網(wǎng)址:http://www.muchs.cn/article18/phjggp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、ChatGPT、微信小程序、全網(wǎng)營銷推廣、服務(wù)器托管、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)