【问题标题】:Multidimensional JSON Array In PHP?PHP中的多维JSON数组?
【发布时间】:2020-06-21 14:29:00
【问题描述】:

我对 JSON 不是很熟悉,更不用说尝试将值从其中提取到 PHP 中,所以在尝试这样做时我有点头疼。

如果这不是嵌套/多维数组,这将是一个简单的搜索、复制和粘贴作业,但我遇到的问题是我只能从数组中提取第一个结果.当然,我的代码有点乱,因为我一直在尝试新方法并对其进行破解以试图获得预期的结果,但我不确定如何以一种拉 的方式处理数据 我正在寻找的信息。

非常感谢您提供的任何提示/建议,以指导我获取所有结果,而不仅仅是第一个结果。我确定这很简单,我只是没有看到,但我已经做了一段时间了,最​​后决定寻求一些帮助。

这是我目前正在使用的 PHP 编码,它只提取一个结果:

$url = "https://somewhere.com/give-api/v1/donors/?key=***&token=***&number=999";
$data = file_get_contents($url);
$donors = json_decode($data, true);
$count = 0;
$output = array();

foreach ($donors as $donor) {
  $spent = substr($donor[$count]['stats']['total_spent'], 0, strpos($donor[$count]['stats']['total_spent'], "."));
  if ($spent != 0)
  {
    $level = check_spent($spent);
    //       ^ Custom function, just returns a level number based on the $spent value
    $output += [$spent, $level, $donor[$count]['info']['first_name'] . " " . $donor[$count]['info']['last_name']];
  }
  $count++;
}

echo '<pre>';
print_r($output);
echo '</pre>';

这是数据的匿名版本:

{
"donors": [
    {
        "info": {
            "user_id": "",
            "username": "",
            "display_name": "",
            "donor_id": "8",
            "title_prefix": "",
            "first_name": "John",
            "last_name": "Doe",
            "email": "john@doe.com"
        },
        "stats": {
            "total_donations": "1",
            "total_spent": "40.000000"
        },
        "address": {
            "billing": [
                {
                    "line1": "1234 Some Pl",
                    "line2": "",
                    "city": "Somewhere",
                    "state": "DC",
                    "country": "US",
                    "zip": "12345"
                }
            ]
        }
    },
    {
        "info": {
            "user_id": "",
            "username": "",
            "display_name": "",
            "donor_id": "7",
            "title_prefix": "",
            "first_name": "John",
            "last_name": "Doe",
            "email": "john@doe.com"
        },
        "stats": {
            "total_donations": "1",
            "total_spent": "50.000000"
        },
        "address": {
            "billing": [
                {
                    "line1": "1234 Some Pl",
                    "line2": "",
                    "city": "Somewhere",
                    "state": "DC",
                    "country": "US",
                    "zip": "12345"
                }
            ]
        }
    },
    {
        "info": {
            "user_id": "",
            "username": "",
            "display_name": "",
            "donor_id": "6",
            "title_prefix": "",
            "first_name": "John",
            "last_name": "Doe",
            "email": "john@doe.com"
        },
        "stats": {
            "total_donations": "1",
            "total_spent": "100.000000"
        },
        "address": {
            "billing": [
                {
                    "line1": "1234 Some Pl",
                    "line2": "",
                    "city": "Somewhere",
                    "state": "DC",
                    "country": "US",
                    "zip": "12345"
                }
            ]
        }
    },
    {
        "info": {
            "user_id": "",
            "username": "",
            "display_name": "",
            "donor_id": "5",
            "title_prefix": "",
            "first_name": "John",
            "last_name": "Doe",
            "email": "john@doe.com"
        },
        "stats": {
            "total_donations": "0",
            "total_spent": "0.000000"
        },
        "address": []
    },
    {
        "info": {
            "user_id": "",
            "username": "",
            "display_name": "",
            "donor_id": "4",
            "title_prefix": "",
            "first_name": "John",
            "last_name": "Doe",
            "email": "john@doe.com"
        },
        "stats": {
            "total_donations": "0",
            "total_spent": "0.000000"
        },
        "address": []
    },
    {
        "info": {
            "user_id": "",
            "username": "",
            "display_name": "",
            "donor_id": "3",
            "title_prefix": "",
            "first_name": "John",
            "last_name": "Doe",
            "email": "john@doe.com"
        },
        "stats": {
            "total_donations": "1",
            "total_spent": "50.000000"
        },
        "address": {
            "billing": [
                {
                    "line1": "1234 Some Pl",
                    "line2": "",
                    "city": "Somewhere",
                    "state": "DC",
                    "country": "US",
                    "zip": "12345"
                }
            ]
        }
    },
    {
        "info": {
            "user_id": "",
            "username": "",
            "display_name": "",
            "donor_id": "2",
            "title_prefix": "",
            "first_name": "John",
            "last_name": "Doe",
            "email": "john@doe.com"
        },
        "stats": {
            "total_donations": "1",
            "total_spent": "50.000000"
        },
        "address": {
            "billing": [
                {
                    "line1": "1234 Some Pl",
                    "line2": "",
                    "city": "Somewhere",
                    "state": "DC",
                    "country": "US",
                    "zip": "12345"
                }
            ]
        }
    }
],
"request_speed": 0.0026030540466308594
}

【问题讨论】:

  • JSON 只是数组和对象的文本表示,没有太多“熟悉”的地方。您只需使用 json_decode()json_encode() 在 JSON 和它所代表的数据之间进行转换。因此,如果您了解 PHP 数组,您就了解 JSON。
  • 尝试将$output +=更改为$output [] =
  • 谢谢你,奈杰尔!与其他建议的编辑相结合,效果非常好!

标签: php arrays json multidimensional-array


【解决方案1】:

看看json_decode之后得到的数据,你可能会得到一个只有一个键的数组:donors

print_r($donors);

如果是这样,请在迭代时执行此操作:

foreach ($donors['donors'] as $donor) {
  // do your work
}

【讨论】:

  • 非常感谢!这与奈杰尔的技巧相结合,效果非常好!
【解决方案2】:

$donors 是整个地图,$donors["donors"] 是你需要迭代的地方

【讨论】:

  • 非常感谢!这与奈杰尔的技巧相结合,效果非常好!
猜你喜欢
  • 2013-10-27
  • 1970-01-01
  • 2019-01-14
  • 1970-01-01
  • 2017-03-27
  • 1970-01-01
  • 2021-05-30
  • 2019-10-14
  • 2016-05-23
相关资源
最近更新 更多