php parsing json data instances

  • 2021-07-12 05:21:15
  • OfStack

This article shows the method of php analyzing json data in the form of examples, which is a more practical function to share for your reference. The specific code is as follows:


<?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.= "]";
// Analyse   The premise is that $data Yes json Format 
$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);
?>

Interested friends can debug 1 example of this article, and I believe there will be new gains.


Related articles: