【发布时间】:2017-08-26 22:09:20
【问题描述】:
我会尽力解释并保持简短。我正在解析一个 JSON 文件。我已经根据 JSON 中的键创建了变量并循环遍历它。我已经有一个 array_filter 过滤我想要允许的循环中的值。我不确定如何更进一步并通过不同的键/变量的值进行另一次检查和禁止。
在我的 JSON 中,我有这个键/对象...
"geocode": {
"UGC": [
"TXZ179",
],
现在我为此创建的用于获取此值的变量是这样的......
$geocode = $currFeature['properties']['geocode']['UGC'][0];
现在,如果您注意到它的值
TXZ179
我只需要该值的前两个字母,因此我将其剥离以仅显示前两个字母...
$state = substr($geocode, 0, -4);
这给我留下了上面的 TX 值。
我解释了这一点。我想获取那个 $state 变量并过滤掉并省略我的循环中的值 = TX 从那个变量。
在遍历解析的 JSON 之前,我会按我想要显示的值对其进行过滤...
//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];
});
这个问题必须通过某个值来过滤 OUT,同时已经有一个现有的 array_filter 来允许某些值,以及它们如何共存。
所以我的问题是由于缺乏知识。我不确定要使用什么函数或如何通过 $state 变量中的 TX 值来禁止。据我所知,array_filter 用于过滤掉您想要保留的值。那么如何在循环抛出它并通过第一个 array_filter 之前使用 TX 删除值。
【问题讨论】:
-
您尚未尝试执行此任务。您要求我们为您编写代码。请先尝试并告诉我们您遇到的问题。使用
substr($geocode,0,2)获取前两个。您的问题没有显示任何研究或尝试自我解决,并且可能会被否决。请更新您的问题。 -
在深度值上过滤多维数组之前已经问过。
-
虽然您的问题提供了针值 (
$state),但它不提供干草堆值 ($features)。我希望当你一周后回来时,你会在你的问题中包含这个,以便它们都包含MCVE。如果您编辑有关此项目的其他最近问题,我将很乐意将它们全部投票为完整的问题。我不是在攻击你,我在支持你和 SO。 (你不在的时候我不会编辑你的问题——这会适得其反,我希望可以合作完成。)
标签: php