doujin4031 2011-12-17 08:14
浏览 16
已采纳

使用存储在数组中的比较运算符

I have the following example array:

$comparisons = array(
    0 => array(
        'method'    => 'productTotalQuantity',
        'operator'  => '>=',
        'value'     => '2'
    ),
    1 => array(
        'method'    => 'productTotalWeight',
        'operator'  => '<=',
        'value'     => '10'
    )
);

I have invented this array structure, so it can be altered if required. I am trying to somehow evaluate the operator key, so that I can achieve something along the lines of:

foreach ($comparisons as $comparison) {
    $value = $this->$comparison['method']($product);
    // E.g. $value = $this->productTotalQuantity($product)
    // $value could = 4

    if ($value $comparison['operator'] $comparison['value']) {
        // Comparison successful
        $matches[] = TRUE;
    }
}

if (count($matches) == count($comparisons)) {
    // All comparisons were successful. Apply the discount.
}

If you have time, a full code example of how to parse this array would be extremely helpful. I've been working on it for about 20 hours straight and think I'm about to literally bash my head against a brick wall. If you are familiar with Magento, I am trying to somewhat mimic the functionality of the "Shopping Cart Price Rule" Conditions in Promotions.

  • 写回答

3条回答 默认 最新

  • doujiao6507 2011-12-17 08:33
    关注

    "translating" operations from strings to real operations is mostly dirty and or evil :-) you need a lot of conditional code (if-else) or eval (but everybody knows: eval is evil ^^)

    I would use an object-oriented approach, since PHP does support object orientation (I don't know the exact php syntax, it has been a while ;-)) and I give you pseudo code to enforce the ideas:

    class AbstractComparator {
       boolean static compare($operand1, $operand2);   
    }
    
    class EqualsComparator extends AbstractComparator {
       @Override
       boolean static compare($operand1, $operand2) {
          return ($operand1 == $operand2);
       }
    }
    
    // Now use one of the subclasses in your datastructure
    $comparisons = array(
    0 => array(
        'method'    => 'productTotalQuantity',
        'operator'  => EqualsComparator,
        'value'     => '2'
    ),
    1 => array(
        'method'    => 'productTotalWeight',
        'operator'  => SmallerThanComparator,
        'value'     => '10'
    )
    );
    
    $allComparisonsSuccessful = true;
    foreach ($comparisons as $comparison) {
       $value = $comparison['operator'].equals($product);
        // Use the AND operator... full predicate is only true if all elements are true
       $allComparisonsSuccessful = $allComparisonsSuccessful && $value;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳