php解析json数组(循环输出数据)的实例

以快递100接口为例

 

返回的JSON数据

 

php解析json数组(循环输出数据)的实例
{"message":"ok","nu":"350116805826","ischeck":"0","com":"huitongkuaidi","status":"1","condition":"00","data":[{"time":"2014-02-12 15:21:45","location":"","context":"合肥市|到件|到合肥市【合肥分拨中心】"},{"time":"2014-01-20 15:20:39","location":"","context":"芜湖市|签收|芜湖市【芜湖分拨中心】, 本人 已签收"},{"time":"2014-01-19 11:23:50","location":"","context":"芜湖市|派件|芜湖市【芜湖分拨中心】,【芜湖BEX代派1】正在派件"},{"time":"2014-01-17 07:51:00","location":"","context":"芜湖市|到件|到芜湖市【芜湖分拨中心】"},{"time":"2014-01-16 22:40:48","location":"","context":"合肥市|发件|合肥市【合肥分拨中心】,正发往【芜湖分拨中心】"},{"time":"2014-01-16 19:32:08","location":"","context":"合肥市|到件|到合肥市【合肥分拨中心】"},{"time":"2014-01-15 23:57:45","location":"","context":"北京市|发件|北京市【北京分拨中心】,正发往【合肥分拨中心】"},{"time":"2014-01-15 23:57:44","location":"","context":"北京市|到件|到北京市【北京分拨中心】"},{"time":"2014-01-15 19:55:25","location":"","context":"北京市|收件|北京市【车公庄分部】,【张灵林(承包区)13391722019】已揽收"}],"state":"0"}

php解析json数组(循环输出数据)的实例

建立函数

php解析json数组(循环输出数据)的实例
function vget($url)
{
    
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  $data = curl_exec($curl);
  if (curl_errno($curl)) {return ‘ERROR ‘.curl_error($curl);}
  curl_close($curl);
   return $data ;
}    
php解析json数组(循环输出数据)的实例

 

PHP代码 转换成数组

php解析json数组(循环输出数据)的实例
$url=‘http://api.kuaidi100.com/api?id=密钥d32&com=huitongkuaidi&nu=350116805826&show=0&muti=1&order=desc‘;
$json=vget($url);

 $obj=json_decode($json);

  $data=$obj->data;
  $i=0;
   foreach ( $data as $unit )
   {
       $i++;
       $arr[$i][‘context‘]=$unit->context;
       $arr[$i][‘time‘]=$unit->time;

  }
 // print_r($arr);
php解析json数组(循环输出数据)的实例

 

ecshop输出方式:php文件

 $smarty->assign(‘arr‘, $arr);

模板文件代码

  <ul>
      {foreach from=$arr item=arr name=arr}
            <li> {$arr.time}-{$arr.context}</li>
        {/foreach}  
  </ul>

 

自己编样式吧!

 

艾丝凡

php解析json数组(循环输出数据)的实例,布布扣,bubuko.com

php解析json数组(循环输出数据)的实例

上一篇:校门外的树(离散化)


下一篇:Xnip Mac上方便好用的截图工具