我有一个JSON文件,每页限制50个项目,总共有818个项目。
我可以单独从前50个数据中提取数据。 我想知道是否有人知道如何拉出所有其他页面,所以他们都在一个PHP请求。
有没有人碰巧有一个功能的想法,或者如果有可能帮助解决这个问题的陈述?
结果数量有限的json示例(将显示50个):
{ "count": 818, "max_per_page": 50, "current_page": 1, "links": [ { "href": "http://domain.com/?page=2", "rel": "next" }, { "href": "http://domain.com/", "rel": "prev" } ], "results": [ { "id": "1", "href": "http://domain.com/23385", "product_line": "A", "departures_start_date": "2015-01-01", "departures_end_date": "2017-12-24" }, { "id": "2", "href": "http://domain.com/22760", "product_line": "B", "departures_start_date": "2015-01-01", "departures_end_date": "2017-12-16" }, { "id": "3", "href": "http://domain.com/22790", "product_line": "C", "departures_start_date": "2015-01-01", "departures_end_date": "2017-12-30" }, ] }I have a JSON file that limits me to 50 items per page, and there is a total of 818 for this one item.
I can pull the data from the first 50 individually. I'm wondering if anyone knows how I can pull all of the additional pages too so they're all in one php request.
Does anyone happen to have an idea for a function or if statement that could help sort this out?
Example piece of json with limited amount of results (50 would be shown):
{ "count": 818, "max_per_page": 50, "current_page": 1, "links": [ { "href": "http://domain.com/?page=2", "rel": "next" }, { "href": "http://domain.com/", "rel": "prev" } ], "results": [ { "id": "1", "href": "http://domain.com/23385", "product_line": "A", "departures_start_date": "2015-01-01", "departures_end_date": "2017-12-24" }, { "id": "2", "href": "http://domain.com/22760", "product_line": "B", "departures_start_date": "2015-01-01", "departures_end_date": "2017-12-16" }, { "id": "3", "href": "http://domain.com/22790", "product_line": "C", "departures_start_date": "2015-01-01", "departures_end_date": "2017-12-30" }, ] }最满意答案
假设你使用file_get_contents检索你的数据,你可以试试这个:
$array = array(); $page = 1; while( $data = file_get_contents( '/Your/Path/Here?page=$page++' ) ) { $json = json_decode( $data, True ); #01 foreach( $json['results'] as $row ) $array[] = $row; }在循环结束时,数组将包含所有元素:
[ [ "id": "1", "href": "http://domain.com/23385", (...) ], [ "id": "2", "href": "http://domain.com/22760", (...) ], (...) ]如果你喜欢用对象而不是数组,你可以忽略True (#01),然后每行可以通过$row->id而不是$row['id']进行调用
Assuming that you retrieve your data using file_get_contents, you can try this:
$array = array(); $page = 1; while( $data = file_get_contents( '/Your/Path/Here?page=$page++' ) ) { $json = json_decode( $data, True ); #01 foreach( $json['results'] as $row ) $array[] = $row; }At the end of the loop, the array will contain all elements:
[ [ "id": "1", "href": "http://domain.com/23385", (...) ], [ "id": "2", "href": "http://domain.com/22760", (...) ], (...) ]If you prefer objects instead of arrays, you can omit True (#01) and then each row will be callable through $row->id instead of $row['id']
更多推荐
发布评论