【发布时间】: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