如何从php中的数据字符串中提取一系列变量?

时间:2019-05-03 13:46:48

标签: php

以下数据块是由网页与其他html数据一起产生的;

UIbuttons

如何从该字符串中提取多个变量,例如“ id”,“ rating”等数据,以便能够将其打印在另一个php页面上?

1 个答案:

答案 0 :(得分:0)

您可以使用json_decode将JSON转换为数组,然后遍历数组

$json = '{
"search":
 {
 "searchResults":
 {
    "results":[
            {"id":"123","name":"ABC","rating":{"average":0,"count":2,"__typename":"Rating"},"category":"AAA/Cars","__typename":"ProductQuery"},
            {"id":"456","name":"DEF","rating":{"average":5,"count":8,"__typename":"Rating"},"category":"BBB/Bikes","__typename":"ProductQuery"}
    ]
  }
 }
}';
$jsonToArray = json_decode($json,true);
foreach($jsonToArray['search']['searchResults']['results'] as $v){
   echo $v['id'];
  print_r($v['rating']);echo '<br/>';
}