PHP-按值筛选并在循环中忽略

PHP-按值筛选并在循环中忽略,php,Php,我将尽我最大的努力解释这一点,并保持简短。我正在解析一个JSON文件。我已经基于JSON中的键创建了变量,并在其中循环。我已经有了一个数组过滤器,可以过滤我想要允许的循环中的值。我不确定的是如何更进一步,进行另一次检查,并通过不同键/变量的值禁用 在我的JSON中,我有一个键/对象 "geocode": { "UGC": [ "TXZ179", ], 现在我为这个值创

我将尽我最大的努力解释这一点,并保持简短。我正在解析一个JSON文件。我已经基于JSON中的键创建了变量,并在其中循环。我已经有了一个数组过滤器,可以过滤我想要允许的循环中的值。我不确定的是如何更进一步,进行另一次检查,并通过不同键/变量的值禁用

在我的JSON中,我有一个键/对象

"geocode": {
                    "UGC": [
                        "TXZ179",
                    ],
现在我为这个值创建的变量是这样的

$geocode = $currFeature['properties']['geocode']['UGC'][0];
$state = substr($geocode, 0, -4);
//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];
});
现在如果你注意到它的值

TXZ179

我只需要从该值中提取前两个字母,所以我将其拆分为仅显示前两个字母,如

$geocode = $currFeature['properties']['geocode']['UGC'][0];
$state = substr($geocode, 0, -4);
//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];
});
这就给我留下了上面的TX值

我解释这个就是为了解释这个。我想取$state变量,过滤掉并省略循环中该变量中=TX的值

在我循环使用解析的JSON之前,我通过我想显示的值对其进行过滤,如下所示

$geocode = $currFeature['properties']['geocode']['UGC'][0];
$state = substr($geocode, 0, -4);
//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];
});
这个问题需要更多地考虑按某个值进行过滤,同时已有一个数组_过滤器允许按某个值进行过滤,以及它们如何共存


所以我的问题是由于缺乏知识。我不确定使用什么函数,也不知道如何通过$state变量中的TX值来禁止。从我的知识数组中,\u filter用于筛选出要保留的值。那么,在循环抛出TX并使其通过第一个数组过滤器之前,如何使用TX移除值呢

您可以扩展现有的过滤功能,也可以根据要素的地理信息排除要素

// 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;
});
如果希望数组项(
$feature
)位于结果数组中,则filter函数只是一个应返回
true
的函数;如果希望从结果数组中排除数组项,则应返回
false


在您的情况下,如果数组项的事件是白名单事件之一(
$alerts
),并且如果状态代码(
$state
)不是
TX

,则希望该数组项在生成的数组中。您要求我们为您编写代码。请先试试,告诉我们你被困在哪里了。使用
substr($geocode,0,2)
获取前两个。你的问题没有研究或试图自我解决,可能会被否决。请更新您的问题。以前曾询问过根据深度值筛选多维数组。虽然您的问题提供了指针值(
$state
),但它没有提供草堆值(
$features
)。我希望当你一周后回来的时候,你会在你的问题中包括这一点,这样它们都会包含一个完整的答案。如果你编辑了你最近关于这个项目的其他问题,我会很高兴把它们都选为完整的问题。我不是在攻击你,我是在支持你等等。(你不在的时候,我不会编辑你的问题——这会对我希望通过合作实现的目标产生反作用。)“$isValidState”做什么?我已经在使用$state变量执行substr(),以缩减$geocode变量,以返回geocode值的前两个字符。$isValidState是执行相同的操作还是执行不同的操作。PHP substr创建新字符串,然后进行比较,使用strncmp的代码速度更快,占用的内存更少。这可能更具可读性,但为什么会影响性能并使用更多堆栈呢?:)我有$state=substr($geocode,0,-4);在我的循环中,它做同样的事情。我是否应该只添加$忽略我的循环中的那个吗?Marcin,在这次讨论中最好不要陷入微观优化兔子洞。Texan78,下次当你“已经在做”某事时,请在问题中显示出来,这样我们才能正确地帮助你。@Texan78在问题中你声明你正在某处使用
substr()
,这很好。但是,在示例代码中,这条小消息没有出现,甚至没有
//我想在这里使用$state
注释。然后在这里的评论中你说,“我有$state…在我的循环中”,这同样很好,但不是你展示给我们的。不管怎么说,这都是顺理成章的。你还有其他问题吗?