【问题标题】:How to simplify array in php如何在php中简化数组
【发布时间】:2020-04-15 16:19:34
【问题描述】:

我有以下数组,我想简化数组以添加我的逻辑来检查产品数量

数组如下:-

$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 operatorvalue 如下:

if(product_qty >= 99){
//do your stuff
}

请帮我简化这个数组以添加我的条件来检查产品数量。

【问题讨论】:

  • 您的数组是否很大并检查元素或只是想访问值?
  • 是的,我的数组很大,我想按照说明检查条件

标签: php arrays multidimensional-array


【解决方案1】:

如果我对您的问题的理解正确。

你可以像这样使用数组索引:

foreach($conditions['conditions'] as $condition){

    if($condition['attribute'] == 'product_qty' && $condition['value']  >= 99 ){
        //do your stuff
    }

}

此代码检查attribute 是否等于product_qtyvalue 是否大于等于 99。条件为真。

【讨论】:

  • @PurushotamSharma 我对你想要什么的理解正确吗?
  • 我不这么认为
  • @PurushotamSharma 当attribute 等于product_qty 并且此项目的value 大于等于99。条件为真。是不是你想要的?
  • 请在我的问题中查看我的条件示例
  • 还有其他解决办法吗?
【解决方案2】:

我认为你最好创建一个以数组作为输入的函数。 它会为每个提到的 ttrasn 做一个:

foreach($conditions['conditions'] as $condition){

    if($condition['attribute'] == 'product_qty' && $condition['value']  >= 99 ){
        //do your stuff
    }

}

但如果 $condition 是一个数组,则递归调用与该数组相同的函数。

【讨论】:

    【解决方案3】:

    您没有简单的方法来与存储在值中的运算符进行比较,因此您需要自己编写函数。一个简单的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'");
        }
    }
    

    要评估您的整个条件,您可以使用递归:

    • 如果是一个值的比较,使用上面写的函数
    • 如果是聚合,则评估所有子节点并定义聚合规则:

    这是一个骨架:

    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
            {
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-15
      • 2015-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多