【发布时间】:2020-08-27 12:24:03
【问题描述】:
首先,让我告诉你,我是 PHP 新手,我已经搜索以了解我想要做什么,但找不到任何解决方案,这就是我在这里发帖的原因。对于专家开发人员来说,我的问题可能看起来很简单,但由于它是一个帮助平台,人们根据他们的专业知识发布他们的问题,所以我也是。如果它伤害了任何专家程序员 EGO,请不要关闭,因为任何善良的程序员都可以看到并帮助我。谢谢
我有一个 JSON 数组,它有多个记录
{"status": "ok", "items": [{"fullname": ["John Doe"], "current_street": "458 moh Hl Z,Jnm, MK M228", "previous_addresses": ["JHt Park Mn, VLK KOL, IU B581", "000 BVH L #5P,BGH, PL3 J210"], "cellphones": ["(000) 000-0001", "(000) 000-0002", "(000) 000-0003", "(000) 000-0004", "(000) 000-0005"], "landlines": [], "emails": [], "url": "https://www.example.com/JN1"}, {"fullname": ["John Doe 2"], "current_street": "000 BVH L #5P,BGH, PL J210", "previous_addresses": ["000 BVH L #5P,BGH, PL1 J210", "000 BVH L #5P,BGH, PL2 J210", "000 BVH L #5P,BGH, PL3 J210", "000 BVH L #5P,BGH, PL4 J210", "000 BVH L #5P,BGH, PL5 J210", "000 BVH L #5P,BGH, PL5 J210", "000 BVH L #5P,BGH, PL6 J210"], "cellphones": [], "landlines": ["(000) 000-0001", "(000) 000-0002", "(000) 000-0003", "(000) 000-0004", "(000) 000-0005"], "emails": ["email1@example.com", "email2@example.com"], "url": "https://www.example.com/JD3"}], "items_dropped": [], "stats": {"crawlera/delay/reset_backoff": 10, "crawlera/request": 10, "crawlera/request/method/GET": 10, "crawlera/response": 10, "crawlera/response/status/200": 10, "downloader/request_bytes": 5500, "downloader/request_count": 10, "downloader/request_method_count/GET": 10, "downloader/response_bytes": 81243, "downloader/response_count": 10, "downloader/response_status_count/200": 10, "elapsed_time_seconds": 3.511176, "finish_reason": "finished", "finish_time": "2020-05-11 18:56:49", "item_scraped_count": 9, "log_count/DEBUG": 19, "log_count/INFO": 12, "memusage/max": 66445312, "memusage/startup": 66445312, "request_depth_max": 1, "response_received_count": 10, "scheduler/dequeued": 10, "scheduler/dequeued/memory": 10, "scheduler/enqueued": 10, "scheduler/enqueued/memory": 10, "start_time": "2020-05-11 18:56:46"}, "spider_name": "friendsearch"}
我需要遍历它们以在 PHP 中回显每条记录,使一条记录可以包含多个 current_address、手机、固定电话值。
基于我的搜索和研究,我已经尝试过
$outputDetails = '{"status": "ok", "items": [{"fullname": ["John Doe"], "current_street": "458 moh Hl Z,Jnm, MK M228", "previous_addresses": ["JHt Park Mn, VLK KOL, IU B581", "000 BVH L #5P,BGH, PL3 J210"], "cellphones": ["(000) 000-0001", "(000) 000-0002", "(000) 000-0003", "(000) 000-0004", "(000) 000-0005"], "landlines": [], "emails": [], "url": "https://www.example.com/JN1"}, {"fullname": ["John Doe 2"], "current_street": "000 BVH L #5P,BGH, PL J210", "previous_addresses": ["000 BVH L #5P,BGH, PL1 J210", "000 BVH L #5P,BGH, PL2 J210", "000 BVH L #5P,BGH, PL3 J210", "000 BVH L #5P,BGH, PL4 J210", "000 BVH L #5P,BGH, PL5 J210", "000 BVH L #5P,BGH, PL5 J210", "000 BVH L #5P,BGH, PL6 J210"], "cellphones": [], "landlines": ["(000) 000-0001", "(000) 000-0002", "(000) 000-0003", "(000) 000-0004", "(000) 000-0005"], "emails": ["email1@example.com", "email2@example.com"], "url": "https://www.example.com/JD3"}], "items_dropped": [], "stats": {"crawlera/delay/reset_backoff": 10, "crawlera/request": 10, "crawlera/request/method/GET": 10, "crawlera/response": 10, "crawlera/response/status/200": 10, "downloader/request_bytes": 5500, "downloader/request_count": 10, "downloader/request_method_count/GET": 10, "downloader/response_bytes": 81243, "downloader/response_count": 10, "downloader/response_status_count/200": 10, "elapsed_time_seconds": 3.511176, "finish_reason": "finished", "finish_time": "2020-05-11 18:56:49", "item_scraped_count": 9, "log_count/DEBUG": 19, "log_count/INFO": 12, "memusage/max": 66445312, "memusage/startup": 66445312, "request_depth_max": 1, "response_received_count": 10, "scheduler/dequeued": 10, "scheduler/dequeued/memory": 10, "scheduler/enqueued": 10, "scheduler/enqueued/memory": 10, "start_time": "2020-05-11 18:56:46"}, "spider_name": "friendsearch"}';
$output = json_decode($outputDetails);
foreach ($output->items as $item){
echo "Name: ".$item->fullname;
echo "<br>";
}
但它向我显示了以下结果:
Name: Array
Name: Array
而我想要达到的目标是:
Name: John Doe
Current Address: 458 moh Hl Z,Jnm, MK M228
Previous Addresses: JHt Park Mn, VLK KOL, IU B581, 000 BVH L #5P,BGH, PL3 J210
Name: John Doe 2
Current Address: 000 BVH L #5P,BGH, PL J210
Previous Addresses: 000 BVH L #5P,BGH, PL1 J210, 000 BVH L #5P,BGH, PL2 J210, 000 BVH L #5P,BGH, PL3 J210, 000 BVH L #5P,BGH, PL4 J210, 000 BVH L #5P,BGH, PL5 J210, 000 BVH L #5P,BGH, PL5 J210, 000 BVH L #5P,BGH, PL6 J210
所以,首先我想循环显示每条记录,然后循环显示它们各自的属性。有什么帮助吗?谢谢
【问题讨论】:
标签: php arrays json loops foreach