【问题标题】:Conditional Inside A Foreach Loop To Filter Value And Have A Fall Back在 Foreach 循环中使用条件过滤值并进行回退
【发布时间】:2018-07-18 09:03:53
【问题描述】:

我有一个奇怪的问题,我无法解决它。我尝试了很多不同的方法来获得预期的结果,但没有成功。我正在寻找的是一组规则,例如如果在循环中满足条件,则回退。所以这就是我迄今为止所做的,但尝试了多种不同的方式。即使定义了国家/地区、邮政编码和运输方式,我总是得到作为匹配项的后备。几乎就像它忽略了这些值。

如果有人能指出正确的方向,我将不胜感激。

$country        = 'GB';
$postCode       = "LE5";
$shippingMethod = "subscription_shipping";

$shippingMatrix = array(
    array(
        "country"            => "GB",
        "isPostCodeExcluded" => "LE5",
        "shippingMethod"     => "subscription_shipping",
        "carrier"            => "Royal Mail",
        "carrierService"     => "Royal Mail",
    ),
    array(
        "country"            => "GB",
        "isPostCodeExcluded" => false,
        "shippingMethod"     => "subscription_shipping",
        "carrier"            => "DHL",
        "carrierService"     => "DHL",
    ),
    array(
        "country"            => false,
        "isPostCodeExcluded" => false,
        "shippingMethod"     => "subscription_shipping",
        "carrier"            => "Fallback",
        "carrierService"     => "Fallback",
    ),
    array(
        "country"            => "GB",
        "isPostCodeExcluded" => false,
        "shippingMethod"     => "standard_delivery",
        "carrier"            => "DPD",
        "carrierService"     => "DPD",
    ),
);

$carriers = [];
foreach ($shippingMatrix as $matrix) {
    // If only Shipping Method is matched then fall back will be the result
    if ($shippingMethod === $matrix['shippingMethod']) {
        $carriers = [
            $matrix['carrier'],
            $matrix['carrierService'],
        ];
        // If only Shipping Method & Country is matched then fall back will be the result DHL
        if ($country === $matrix['country']) {
            $carriers = [
                $matrix['carrier'],
                $matrix['carrierService'],
            ];
            // If only Shipping Method & Country & PostCode is matched then fall back will be the result Royal Mail
            if ($postCode === $matrix['isPostCodeExcluded']) {
                $carriers = [
                    $matrix['carrier'],
                    $matrix['carrierService'],
                ];
            }
        }
    }
}

var_dump($carriers);

【问题讨论】:

    标签: if-statement multidimensional-array foreach filtering conditional-statements


    【解决方案1】:

    感谢您的回复,但这不是必需的。经过一个很好的旧想法之后,我犯了将我的类型混合在一起的新手错误。

    $carriers = [];
    foreach ($shippingMatrix as $matrix) {
        if ($shippingMethod === $matrix['shippingMethod'] && $country === $matrix['country']) {
            if ($postCode === $matrix['isPostCodeExcluded']) {
                $carriers = [
                    $matrix['carrier'],
                    $matrix['carrierService'],
                ];
            }
        }
    
        if ($shippingMethod === $matrix['shippingMethod'] && $country !== $matrix['country'] && !$country) {
            $carriers = [
                $matrix['carrier'],
                $matrix['carrierService'],
            ];
        }
    }
    

    【讨论】:

      【解决方案2】:

      $carriers 变量未用作数组表示法,因此 值被覆盖。您只需将 $carriers 转换为 $carriers[] 即可。

      下面是示例。

      $carriers[]  = [
                  $matrix['carrier'],
                  $matrix['carrierService'],
                ];
      

      【讨论】:

        猜你喜欢
        • 2018-02-18
        • 2013-01-16
        • 2012-08-25
        • 2015-06-06
        • 2015-04-16
        • 2014-02-13
        • 1970-01-01
        • 2020-06-17
        相关资源
        最近更新 更多