我在数组下面,我想简化数组以添加检查产品数量的逻辑
数组如下:-
$conditions = Array
(
[type] => Magento\SalesRule\Model\Rule\Condition\Combine
[attribute] =>
[operator] =>
[value] => 1
[is_value_processed] =>
[aggregator] => all
[conditions] => Array
(
[0] => Array
(
[type] => Magento\SalesRule\Model\Rule\Condition\Product\Found
[attribute] =>
[operator] =>
[value] => 1
[is_value_processed] =>
[aggregator] => all
[conditions] => Array
(
[0] => Array
(
[type] => Magento\SalesRule\Model\Rule\Condition\Product
[attribute] => category_ids
[operator] => ==
[value] => 5
[is_value_processed] =>
)
[1] => Array
(
[type] => Magento\SalesRule\Model\Rule\Condition\Product
[attribute] => category_factor
[operator] => ==
[value] => 13
[is_value_processed] =>
)
)
)
[1] => Array
(
[type] => Magently\CustomerRule\Model\Rule\Condition\Customer
[attribute] => product_qty
[operator] => >=
[value] => 99
[is_value_processed] =>
)
)
)
我要检查attribute
operator
和value
,如下所示:
if(product_qty >= 99){
//do your stuff
}
请帮助我简化此数组以添加我的条件以出于某些目的检查产品数量。
答案 0 :(得分:0)
如果我正确理解您的问题。
您可以像这样使用数组索引:
foreach($conditions['conditions'] as $condition){
if($condition['attribute'] == 'product_qty' && $condition['value'] >= 99 ){
//do your stuff
}
}
此代码检查attribute
等于product_qty
且此项的value
等于99以上。条件为true。
答案 1 :(得分:0)
我认为您最好创建一个将数组作为输入的函数。 它将为每个提及的ttrasn做一个
foreach($conditions['conditions'] as $condition){
if($condition['attribute'] == 'product_qty' && $condition['value'] >= 99 ){
//do your stuff
}
}
但是,如果$ condition是一个数组,请对该数组递归调用同一函数。
答案 2 :(得分:0)
您没有一种简单的方法可以与存储在值中的运算符进行比较,因此您需要自己编写函数。一个简单的 day | d01 | d02 | d03 | d04 | d05 | d06 | d07 | d08 | d09 | d10 | d11 | d12 | d13 | d14 | d15 | d16 | d17 | d18 | d19 | d20 | d21 | d22 | d23
-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----
0 | 0 | 0 | 333 | 0 | 777 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
就可以完成这项工作:
switch
要评估您的整体情况,可以使用递归:
这里是骨架:
function compareValue($variable, $operator, $value){
switch($operator)
{
case '==' : return $variable == $value ; break ;
case '>=' : return $variable >= $value ; break ;
/* add all possibilities here */
default : throw new RuntimeException("Undefined operator '$operator'");
}
}