php利用嵌套數(shù)組拼接與解析json的方法
利用嵌套數(shù)組 拼接混合json -包含對(duì)象數(shù)組
<?php // 自 PHP 5.4 起 $array = [ "status" => "0", "message" => "ok", "arr"=> [] ]; class Person { public $name; public $age; //定義一個(gè)構(gòu)造方法初始化賦值 public function __construct($name,$age) { $this->name=$name; $this->age=$age; } } for($i=0;$i<10;$i++) { $p=new Person("ren",$i); $array["arr"][]=$p; } //var_dump($array); echo json_encode($array); ?>
php利用嵌套數(shù)組 解析混合json 包含對(duì)象數(shù)組
<?php function json_to_array($web) { $arr=array(); foreach($web as $k=>$v) { if(is_object($v)) $arr[$k]=json_to_array($v); //判斷類型是不是object else $arr[$k]=$v; } return $arr; } $s='{"webname":"homehf","url":"www.homehf.com","qq":"744348666"}'; //將字符轉(zhuǎn)成JSON $web=json_decode($s); $arr=array(); foreach($web as $k=>$v) $arr[$k]=$v; echo "<pre>"; print_r($arr); echo "</pre>"; $s='{"webname":"homehf","url":"www.homehf.com","contact":{"qq":"744348666","mail":"nieweihf@163.com","xx":"xxxxxxx"}}'; $web=json_decode($s); $arr=json_to_array($web); echo "<pre>"; print_r($arr); echo "</pre>"; /************************************************************************ ************************************************************************/ $s='{"webname":"homehf","url":"www.homehf.com","contact":{"qq":"744348666","mail":"nieweihf@163.com","xx":"xxxxxxx"}}'; $web=json_decode($s); echo '網(wǎng)站名稱:'.$web->webname.'<br />網(wǎng)址:'.$web->url.'<br />聯(lián)系方式:QQ-'.$web->contact->qq.' MAIL:'.$web->contact->mail; echo '<br /><br />'; /************************************************************************ ************************************************************************/ $s='{"webname":"homehf","url":"www.homehf.com","contact":{"qq":"744348666","mail":"nieweihf@163.com","xx":"xxxxxxx"}}'; $web=json_decode($s); echo json_encode($web); $mys='{"status":"0","message":"ok","arr":[{"name":"ren","age":0},{"name":"ren","age":1},{"name":"ren","age":2}, {"name":"ren","age":3},{"name":"ren","age":4},{"name":"ren","age":5},{"name":"ren","age":6},{"name":"ren","age":7}, {"name":"ren","age":8},{"name":"ren","age":9}]}'; $myweb=json_decode($mys); echo $myweb->status; for($i=0;$i<10;$i++) { echo $myweb->arr[$i]->age; echo '<br /><br />'; } ?>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Laravel Validator自定義錯(cuò)誤返回提示消息并在前端展示
這篇文章主要給大家介紹了關(guān)于Laravel Validator自定義錯(cuò)誤返回提示消息并在前端展示的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Laravel具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05求幫忙修改個(gè)php curl模擬post請求內(nèi)容后并下載文件的解決思路
這篇文章主要介紹了求幫忙修改個(gè)php curl模擬post請求內(nèi)容后并下載文件的解決辦法的相關(guān)資料,需要的朋友可以參考下2015-09-09thinkPHP統(tǒng)計(jì)排行與分頁顯示功能示例
這篇文章主要介紹了thinkPHP統(tǒng)計(jì)排行與分頁顯示功能,結(jié)合實(shí)例形式分析了thinkPHP數(shù)據(jù)庫查詢與結(jié)果分頁顯示相關(guān)操作技巧,需要的朋友可以參考下2016-12-12PHP中nowdoc和heredoc使用需要注意的一點(diǎn)
多次使用 php nowdoc HereDoc 插入大量Hmtl都沒有成功,一樣提示語法有問題,事實(shí)上PHP手冊注明是這樣寫的,實(shí)在很奇怪2014-03-03ThinkPHP5中如何實(shí)現(xiàn)模板完全靜態(tài)化詳解
這篇文章主要為大家介紹了ThinkPHP5中如何實(shí)現(xiàn)模板完全靜態(tài)化詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05laravel validate 設(shè)置為中文的例子(驗(yàn)證提示為中文)
今天小編就為大家分享一篇laravel validate 設(shè)置為中文的例子(驗(yàn)證提示為中文),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09