如何简化php中的数组
我有下面的数组,我想简化数组,添加我的逻辑来检查产品数量 阵列如下所示:-如何简化php中的数组,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我有下面的数组,我想简化数组,添加我的逻辑来检查产品数量 阵列如下所示:- $conditions = Array ( [type] => Magento\SalesRule\Model\Rule\Condition\Combine [attribute] => [operator] => [value] => 1 [is_value_processed] =>
$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] =>
)
)
)
我想检查属性
运算符
和值
如下:
if(product_qty >= 99){
//do your stuff
}
请帮助我简化此数组以添加我的条件,以便出于某种目的检查产品数量。如果我正确理解了您的问题 可以像这样使用数组索引:
foreach($conditions['conditions'] as $condition){
if($condition['attribute'] == 'product_qty' && $condition['value'] >= 99 ){
//do your stuff
}
}
当
属性
等于产品数量
且此项目的值
大于99时,此代码进行检查。条件为true。我认为最好创建一个以数组作为输入的函数。
它将为所提到的每个ttrasn做一个测试:
foreach($conditions['conditions'] as $condition){
if($condition['attribute'] == 'product_qty' && $condition['value'] >= 99 ){
//do your stuff
}
}
但是,如果$condition是一个数组,则递归调用该数组的同一个函数。您没有简单的方法与存储在值中的运算符进行比较,因此您需要自己编写函数。一个简单的
开关
可以完成以下工作:
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'");
}
}
要评估整个条件,可以使用递归:
- 如果它是与值的比较,请使用上面写的函数
- 如果是聚合,请评估所有子项并定义聚合规则:
function checkConditions($product, $condition){
if($condition['attribute'] !== false and $condition['operator'] !== false)
{
// simple condition
$variableToCheck = $product[ $condition['attribute'] ] ; // maybe use your API to query this value
$operator = $condition['operator'] ;
$valueToCompare = $condition['value'] ;
return compareValue($variableToCheck, $operator, $valueToCompare);
}
else
{
// composite condition
if( $condition['aggregator'] == 'all' )
{
// check for each child condition
foreach($condition['conditions'] as $childCondition)
{
$childConditionReturn = checkConditions($product, $childCondition);
if($childConditionReturn === false)
{
// we found a child condition that is not satisfied, the whole 'all' aggregator is false
return false ;
}
}
// all childs were evaluated to true, whole 'all' aggregator is true
return true ;
}
else // handle other aggregator types here
{
}
}
}
您的数组很大,是否检查元素或只是想访问值?是,我的数组很大,我想检查条件为explained@PurushotamSharma我对你想要什么有正确的理解吗?我不认为so@PurushotamSharma当
属性
等于产品数量
且该项的值
大于等于99。情况属实。这是你想要的还是不想要的?请在我的问题中查看我的条件示例。还有其他解决方法吗?