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.

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

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备