【问题标题】:PHP - Filter by value and omit in loop [duplicate]PHP - 按值过滤并在循环中省略[重复]
【发布时间】: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


【解决方案1】:

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

// 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) 在结果数组中,则应该返回 true,或者如果您希望从数组项中排除数组项,则返回 false结果数组。

在您的情况下,如果数组项的事件是列入白名单的事件之一 ($alerts) 并且状态代码 ($state) 不是 TX,则您希望数组项位于结果数组中。

【讨论】:

  • “$isValidState”有什么作用?我已经在使用 $state 变量执行 substr() 来修剪 $geocode 变量以返回地理编码值的前两个字符。 $isValidState 是做同样的事情还是执行不同的事情。
  • PHP substr 创建新字符串,然后比较,使用 strncmp 的代码更快,使用更少的内存。这可能更具可读性,但为什么要阻碍性能并使用更多堆栈呢? :)
  • 我有 $state = substr($geocode, 0, -4);在我的循环中做同样的事情。我应该在循环中添加 != $ignoreState 吗?
  • Marcin,最好不要在这个讨论中潜入微优化兔子洞。 Texan78,下次您“已经在做”某事时,请在问题中显示出来,以便我们为您提供适当的帮助。
  • @Texan78 在问题中你说你正在使用substr() somewhere,这很好。但是,您的示例代码中没有出现这个小花絮,甚至没有 // I'd like to use $state here 注释。然后在这里的 cmets 中,您说“我的循环中有 $state...”,这再次很棒,但不是您向我们展示的内容。无论如何,这一切都是顺其自然。您还有其他问题吗?
猜你喜欢
  • 2018-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多