dousilie9522 2017-08-26 22:09
浏览 80
已采纳

PHP - 按值过滤并在循环中省略[重复]

This question already has an answer here:

I will do my best to explain this and keep it short as well. I am parsing a JSON file. I have created variables based on keys from the JSON and loop through it. I already have one array_filter that filters values in the loop I want to allow. What I am not sure of is how to go a step further and do another check and disallow by the value of a different key/variable.

In my JSON I have this key/object...

"geocode": {
                    "UGC": [
                        "TXZ179",
                    ],

Now the varible I have created for this to get this value is like so...

$geocode = $currFeature['properties']['geocode']['UGC'][0];

Now if you notice the value it is

TXZ179

I only need to the first two letters from that value so I have stripped it down to show only the first two letters like so...

$state = substr($geocode, 0, -4);

This leaves me with the value of TX from above.

I explained that to explain this. I want to take that $state variable and filter out and omit values in my loop that = TX from that variable.

Before I loop through my parsed JSON I filter it by values that I want to SHOW like so...

//Lets filter the response to get only the values we want
$alerts = array(
    'Tornado Warning',
    'Severe Thunderstorm Warning',
    'Hurricane Warning',
    'Tropical Storm Warning',
    'Flash Flood Warning',
    'Flood Warning',
);

// filter features, remove those which are not of any of the desired event types
    $alertFeatures = array_filter($features, function(array $feature) use ($alerts) {
    $eventType = $feature['properties']['event'];

    return in_array($eventType, $alerts);
});

// flip alerts, mapping names of alerts to index in array (we'll use it to order)
$order = array_flip($alerts);

// sort elements by order of event type as desired
usort($alertFeatures, function (array $a, array $b) use ($order) {
    $eventTypeA = $a['properties']['event'];
    $eventTypeB = $b['properties']['event'];

    return $order[$eventTypeA] - $order[$eventTypeB];
});

This question has to do more with filter OUT by a certain value while already having an existing array_filter to ALLOW by certain values and how they can both co-exist.

So my problem is due to the lack of knowledge. I am not sure what function to use or how to disallow by the value of TX in the $state variable. From my knowledge array_filter is for to filter out values you want to keep. So how can I remove values with TX before I loop throw it and it passes the first array_filter.

</div>
  • 写回答

1条回答 默认 最新

  • dongxin991209 2017-08-26 23:15
    关注

    You can augment your existing filter function to also exclude features based on their geo information.

    // this is the TX You don't want
    $ignoreState = 'TX';
    
    // filter features, remove those which are not of any of the desired event types
    // and also remove those from $ignoreState
    $alertFeatures = array_filter($features, function (array $feature) use ($alerts, $ignoreState) {
        $eventType = $feature['properties']['event'];
        $isValidEvent = in_array($eventType, $alerts);
    
        $geocode = $feature['properties']['geocode']['UGC'][0];
        $isValidState = substr($geocode, 0, 2) != $ignoreState;
    
        return $isValidState && $isValidEvent;
    });
    

    The filter function is just a function that is supposed to return true if you want the array item ($feature) to be in the resulting array, or false if you want the array item to be excluded from the resulting array.

    In your case, you want the array item to the in the resulting array if its event is one of the whitelisted events ($alerts) and if the state code ($state) is not TX.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MFC平台生成指定圆
  • ¥15 jmeter出现403
  • ¥500 求华为P30PRO手机硬盘数据恢复
  • ¥15 关于#vscode#的问题:ESP32开发板对接MQTT实现小灯泡的开关
  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据