猿问

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

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


"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"}


{"id": //and so on//

"}


]}}

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


12345678_0001
浏览 191回答 1
1回答

慕侠2389804

您可以json_decode用来将JSON转换为数组,然后遍历数组$json = '{"search":&nbsp;{&nbsp;"searchResults":&nbsp;{&nbsp; &nbsp; "results":[&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {"id":"123","name":"ABC","rating":{"average":0,"count":2,"__typename":"Rating"},"category":"AAA/Cars","__typename":"ProductQuery"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {"id":"456","name":"DEF","rating":{"average":5,"count":8,"__typename":"Rating"},"category":"BBB/Bikes","__typename":"ProductQuery"}&nbsp; &nbsp; ]&nbsp; }&nbsp;}}';$jsonToArray = json_decode($json,true);foreach($jsonToArray['search']['searchResults']['results'] as $v){&nbsp; &nbsp;echo $v['id'];&nbsp; print_r($v['rating']);echo '<br/>';}
随时随地看视频慕课网APP
我要回答