Home  >  Article  >  Backend Development  >  php解析json数据实例_PHP

php解析json数据实例_PHP

WBOY
WBOYOriginal
2016-05-31 19:30:29998browse

JSON

本文以实例形式展示了php解析json数据的方法,这是一个比较实用的功能,分享给大家供大家参考。具体代码如下:

<&#63;php
$data;
$data.= "[";
for ($i = 0; $i < 20; $i++) {
  $data.= "{";
  $data.= "\"id\":\"" . 110 . "\",";
  $data.= "\"name\":\"" . 110 . "\",";
  $data.= "\"original_price\":\"" . 110 . "\",";
  $data.= "\"sale_price\":\"" . 110 . "\",";
  $data.= "\"introduce\":\"" . 110 . "\",";
  $data.= "\"content\":\"" . 110 . "\",";
  $data.= "\"nums\":" . 110 . "";
  $data.= "}";
  if ($i < 19) {
    $data.= ",";
  }
}
$data.= "]";
//解析 前提是$data是json格式
$array = array();
$de_json = json_decode($data, true);
//var_dump($de_json);
$count_json = count($de_json);
for ($i = 0; $i < $count_json; $i++) {
  $array[$i]["id"] = $de_json[$i]['id'];
  $array[$i]["name"] = $de_json[$i]['name'];
  $array[$i]["original_price"] = $de_json[$i]['original_price'];
  $array[$i]["sale_price"] = json_encode($de_json[$i]['sale_price']);
}
var_dump($array);
&#63;>

感兴趣的朋友可以动手调试一下本文实例,相信会有新的收获。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn