First of all, let me tell you that I am new to PHP and I have searched to learn what I am trying to do but could not find any solution, that is why I am posting here. My question may seem very simple to an expert developer but since its a helping platform and people post their issues according to their expertise, so I am too. If it hurts any expert programmers EGO, please do not close because any kind programmer can see and help me. Thanks
I have a JSON array which has multiple records which is
{"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": ["[email protected]", "[email protected]"], "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"}
I need to loop through them to echo each record in PHP in a way that a record can have multiple values of current_address, cellphone, landlines with in them.
For this based on my serach and study, I have tried with
$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": ["[email protected]", "[email protected]"], "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>";
}
But it is showing me below result:
Name: Array
Name: Array
And what I am looking to achieve is something like:
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
So, first I want to loop to show the each record, then loop to show their individual attributes. Any help on it? Thanks