【问题标题】:PHP: How to use array_filter() to filter JSON response values?PHP:如何使用 array_filter() 过滤 JSON 响应值?
【发布时间】:2017-08-13 14:53:24
【问题描述】:

我正在尝试使用 array_filter 过滤某些值并禁止其余值。我以前从未使用过array_filter,所以我一直在努力解决这个问题。

在循环之前我是否需要先过滤这些值?如果需要,我将如何过滤数组以仅查找这些值。

这是我目前正在使用的 sn-p,它正在消除错误。 $alerts 是我只需要从数组中返回的值,而不允许其余的值。然后我遍历过滤后的结果。在我看来,这似乎是合乎逻辑的,但我确信我错过了一些东西,因为它不起作用。

$data = file_get_contents($url); // put the contents of the file into a variable
$results = json_decode($data, true); // decode the JSON feed

$alerts = array(Tornado Warning, Severe Thunderstorm Warning, Flash Flood Warning, Flood Warning);

$selected_events = array_filter($results['features'], function($data) {
        return $data === $alerts;
    });

//Lets loop through the array
foreach($selected_events as $currFeature)
{
    $event = $currFeature['properties']['event'];
    $wfo = $currFeature['properties']['sender'];
    $area = $currFeature['properties']['areaDesc'];
    $expires = $currFeature['properties']['expires'];

这是我正在使用的 JSON 示例。

{
"@context": [
    "https://raw.githubusercontent.com/geojson/geojson-ld/master/contexts/geojson-base.jsonld",
    {
        "wx": "https://api.weather.gov/ontology#",
        "@vocab": "https://api.weather.gov/ontology#"
    }
],
"type": "FeatureCollection",
"features": [
    {
        "id": "https://api.weather.gov/alerts/NWS-IDP-PROD-2485744-2320592",
        "type": "Feature",
        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        -96.77,
                        33.84
                    ],
                    [
                        -96.76,
                        33.82
                    ],
                    [
                        -96.71,
                        33.83
                    ],
                    [
                        -96.52,
                        33.82
                    ],
                    [
                        -96.5,
                        33.77
                    ],
                    [
                        -96.43,
                        33.78
                    ],
                    [
                        -96.38,
                        33.73
                    ],
                    [
                        -96.39,
                        33.45
                    ],
                    [
                        -96.42,
                        33.46
                    ],
                    [
                        -96.94,
                        33.54
                    ],
                    [
                        -96.94,
                        33.85
                    ],
                    [
                        -96.77,
                        33.84
                    ]
                ]
            ]
        },
        "properties": {
            "@id": "https://api.weather.gov/alerts/NWS-IDP-PROD-2485744-2320592",
            "@type": "wx:Alert",
            "id": "NWS-IDP-PROD-2485744-2320592",
            "areaDesc": "Grayson",
            "geocode": {
                "UGC": [
                    "TXC181"
                ],
                "SAME": [
                    "048181"
                ]
            },
            "references": [],
            "sent": "2017-08-13T13:18:17+00:00",
            "effective": "2017-08-13T13:18:17+00:00",
            "onset": "2017-08-13T13:18:00+00:00",
            "expires": "2017-08-13T16:15:00+00:00",
            "ends": "2017-08-13T16:15:00+00:00",
            "status": "Actual",
            "messageType": "Alert",
            "category": "Met",
            "severity": "Severe",
            "certainty": "Likely",
            "urgency": "Expected",
            "event": "Flood Warning",
            "sender": "NWS Fort Worth TX",
            "headline": "Flood Warning issued August 13 at 8:18AM CDT expiring August 13 at 11:15AM CDT by NWS Fort Worth TX",
            "description": "The National Weather Service in Fort Worth has issued a\n\n* Flood Warning for...\nGrayson County in north central Texas...\n\n* Until 1115 AM CDT\n\n* At 816 AM CDT, Emergency management reported flooding of several\nroadways across Grayson County. While the heaviest rainfall has\nended...excessive runoff will continue to result in\nstreams...creeks and some rivers exceeding their banks across the\narea.\n\n* Some locations that will experience flooding include...\nSherman, Denison, Whitesboro, Howe, Pottsboro, Collinsville,\nWhitewright, Bells, Tom Bean, Knollwood, Dorchester, Eisenhower\nState Park, southeastern Lake Texoma, Southmayd, Sadler and Luella.",
            "instruction": "A Flood Warning means that flooding is imminent or occurring. Water\nrises will generally be gradual, but flooding of some low water\ncrossings and other low-lying areas near rivers, creeks, and streams\nis expected.",
            "response": "Avoid",
            "parameters": {
                "VTEC": [
                    "/O.NEW.KFWD.FA.W.0008.170813T1318Z-170813T1615Z/"
                ],
                "EAS-ORG": [
                    "WXR"
                ],
                "PIL": [
                    "FWDFLWFWD"
                ],
                "BLOCKCHANNEL": [
                    "CMAS",
                    "EAS",
                    "NWEM"
                ],
                "eventEndingTime": [
                    "2017-08-13T16:15:00Z"
                ]
            }
        }
    },

【问题讨论】:

    标签: php arrays json


    【解决方案1】:

    $alerts 数组的定义在其当前形式中不正确。您应该将可能的值包含在"' 中,请参阅下面的解决方案。

    您可以执行以下操作:

    $o=json_decode($str,true); // $str is the JSON string
    $features=$o['features'];
    $filtered= array_filter($features,function($el){
        $alerts = array('Tornado Warning', 'Severe Thunderstorm Warning', 'Flash Flood Warning', 'Flood Warning');
        return in_array($el['properties']['event'],$alerts);});
    

    json_encode(...,true) 将产生一个关联数组(而不是一个对象)。然后过滤器函数检查数组$features 中元素的可能['properties']['event'] 属性。只有在$alerts 数组中找到值的那些(我为此使用in_array())才能通过过滤过程。

    如果您愿意,您还可以在实际过滤器函数之外定义 $alerts 数组,并仅在此处引用它:

    ..
    ...
    $alerts = array('Tornado Warning', 'Severe Thunderstorm Warning',
                    'Flash Flood Warning', 'Flood Warning');
    $filtered= array_filter($features,function($el) use ($alerts) {
        return in_array($el['properties']['event'],$alerts);});
    

    你可以在这里找到一个工作演示:http://rextester.com/EELE62798

    【讨论】:

    • 我没有投反对票。我很感激你的回应。很可能是另一个撤回他的答案的人获得了 2 票反对。关于代码,它给了我以下错误。 str 是未定义的,并且 array_filter() 期望参数 1 是数组,null 指的是你的最后一行代码,以 return in_array 开头。
    • 我的$str 应该等同于您的$data。我在 rextester.com 演示的基础上开发了我的解决方案。所以,只要$str 中有一个有效的 JSON 字符串,我的代码就可以工作。您指定的 JSON 字符串实际上缺少结束 ]} - 请参阅我的演示。
    • 嗯,好的,现在对 $str 来说是有意义的。我的监督是有道理的。这也让我发现了另一个问题。我为两个不同的事物定义了两次 $data。这是带有实时 JSON 数据的演示。 rextester.com/HGPOJ47232
    • 嘿,你不能简单地json_decode一个网址!您必须首先获取网站的实际内容。我认为简单的file_get_contents() 不会在这种情况下工作。您可能必须使用 CURL,如下所述:stackoverflow.com/a/12782109/2610061,但在 rextester 上这也不起作用(刚刚测试过)。然而,这是一个不同的问题。如果您仍然遇到问题,您应该单独发布。
    • 该 URL 指向我域中的文件。它是使用 cURL 从 API 中获取的,然后在我的服务器上保存为 .json 文件。在我开始添加 array_filter 之前,它一直在完美地循环遍历数组。
    猜你喜欢
    • 2011-05-14
    • 2021-10-28
    • 2019-05-25
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多