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

以快递100接口为例

 

返回的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"}

建立函数

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代码 转换成数组

$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);

 

ecshop输出方式:php文件

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

模板文件代码

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

 

自己编样式吧!

 

艾丝凡

php解析json数组(循环输出数据)的实例,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。