【问题标题】:Nested loop to show data from JSON array in php嵌套循环以在 php 中显示来自 JSON 数组的数据
【发布时间】: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


    【解决方案1】:

    您构建 JSON 的方式并不完全正确。通过将字符串包装在方括号中,您已将 fullname 定义为数组。而不是["John Doe"],只需使用"John Doe"。这将允许您使用 $item-&gt;fullname 返回名称。

    如果要显示嵌套数组不同级别的值,则需要在结构的每个级别创建一个循环。

    您下面的foreach 仅访问数组的第一级。

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

    如果您想遍历数组中的值(根据您的问题),请在其中添加另一个 foreach 语句:

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

    第二个foreach 在这种情况下是多余的,您可以使用implode 将数组显示为字符串,如下所示:

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

    查看https://jsonlint.com/ 以格式化 JSON 以使其更具可读性。此外,还有一个名为 JSONView 的 Chrome 插件,如果您使用 API,它有助于在浏览器中查看输出。

    【讨论】:

    • 感谢指正。获取后是否可以在PHP端更改JSON结构?
    • 是的,但我不太确定您要更改什么。您可以使用json_encode 将变量转换为 JSON。 $outputDetails 是如何产生的?
    • 现在,我知道为什么当我回显全名时会得到 ARRAY,因为它是一个数组。再次感谢您以简单的方式让我理解复杂的事情。
    【解决方案2】:

    在你的 json 你有

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

    应该是

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

    或者

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-08
      • 2019-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-31
      • 1970-01-01
      相关资源
      最近更新 更多