简体   繁体   中英

Nested loop to show data from JSON array in php

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

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": ["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>";
}

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

The way that you have structured your JSON isn't quite right. You've defined fullname as an array by wrapping the string in square brackets. Instead of ["John Doe"] , just use "John Doe" . That will allow you to return the name using $item->fullname .

If you want to display values from different levels of a nested array, you will need to create a loop at each level of the structure.

Your foreach below only accesses the first level of the array.

foreach ($output->items as $item){
  echo "Name: ".$item->fullname;
  echo "<br>";
}

If you want to loop through the values in an array (as per your question), add another foreach statement inside:

foreach ($output->items as $item){
    echo "Current Address: " . $item->current_street;
    echo "Previous Addresses: ";
    foreach($item->previous_addresses as $address){
        echo $address;
    }
}

The second foreach is overkill in this instance, and you could display the array as a string by using implode , like this:

echo implode(",", $item->previous_addresses);

Have a look at https://jsonlint.com/ for formatting JSON to make it more readable. Also, there's a plugin for Chrome called JSONView, which helps to see output in the browser if you're working with an API.

In your json you have

items":[
{
"fullname":[
"John Doe"
],

And it should be

items":[
{
"fullname":"John Doe",

Or

echo "Name: ".$item->fullname[0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM