国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php利用嵌套數(shù)組拼接與解析json的方法

2024-05-04 23:54:48
字體:
供稿:網(wǎng)友

利用嵌套數(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í)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PHP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 泰州市| 新泰市| 望城县| 平南县| 突泉县| 齐齐哈尔市| 杭锦后旗| 余庆县| 承德县| 济源市| 耒阳市| 克东县| 玉门市| 万源市| 池州市| 华阴市| 武乡县| 成都市| 武威市| 承德市| 高阳县| 正镶白旗| 黄山市| 内乡县| 景德镇市| 浦北县| 舟山市| 天长市| 玛纳斯县| 丽水市| 黔西| 麦盖提县| 阿瓦提县| 韩城市| 东宁县| 云安县| 曲靖市| 滦平县| 昌黎县| 灵石县| 鲁甸县|