如果值匹配,如何比较内部数组值并显示错误消息,如果值不匹配,如何显示成功消息?

时间:2014-05-06 15:16:56

标签: php arrays multidimensional-array associative-array key-value

我有一个名为$rebate_by_product的关联数组。这种关联数组本质上是动态的,这意味着它可以很长或很短,具体取决于它包含的数据。我在下面显示了包含三个回扣条目的小实例。

Array
(
    [op] => preview
    [id] => 
    [form_submitted] => yes
    [company_id] => 46
    [1] => Array
        (
            [pack] => 10
            [quantity] => 20
            [volume] => 30
            [units] => 7
            [amount] => 40
            [rebate_start_date] => 2014-05-01
            [rebate_expiry_date] => 2014-05-15
            [applicable_states] => Array
                (
                    [0] => 1
                    [1] => 6
                    [2] => 11
                    [3] => 16
                    [4] => 20
                )

            [rebate_total_count] => 5000
            [products] => Array
                (
                    [1] => 9
                    [2] => 10
                )

        )

    [2] => Array
        (
            [pack] => 100
            [quantity] => 200
            [volume] => 300
            [units] => 9
            [amount] => 400
            [rebate_start_date] => 2014-05-16
            [rebate_expiry_date] => 2014-05-31
            [applicable_states] => Array
                (
                    [0] => 27
                    [1] => 32
                    [2] => 37
                    [3] => 42
                    [4] => 47
                    [5] => 49
                )

            [rebate_total_count] => 9000
            [products] => Array
                (
                    [1] => 11
                    [2] => 8
                )

        )

    [3] => Array
        (
            [pack] => 1500
            [quantity] => 3000
            [volume] => 4500
            [units] => 10
            [amount] => 6000
            [rebate_start_date] => 2014-06-01
            [rebate_expiry_date] => 2014-06-07
            [applicable_states] => Array
                (
                    [0] => 4
                    [1] => 13
                )

            [rebate_total_count] => 7500
            [products] => Array
                (
                    [1] => 10
                    [2] => 11
                )

        )

    [multiselect] => 13
)

您可以从上面的数组中观察到我将每个回扣条目的产品ID产品存储到密钥[products]下的数组格式中。现在我不想重复这些id,而不管它们所属的回扣条目。也就是说,每个回扣条目必须在密钥[products]下具有不同的产品ID。为实现这一目标,我认为应将每个回扣条目中的每个产品ID与其他产品ID进行比较。可能是我的方法是错的。当您遇到产品ID的第一个匹配时,错误消息应显示为“请选择不同的产品”,并且在比较所有产品ID之后找不到单个匹配项时,成功消息应显示为“所有产品不同”。如何以最少的循环迭代和利用现成的PHP数组函数以最佳方式实现这一目标?感谢您花费一些时间来理解我的问题。如果您想了解有关该问题的任何其他信息,我可以为您提供相同的信息。任何形式的帮助,建议,评论和答案都将受到高度赞赏。等待你宝贵的回复。谢谢。

2 个答案:

答案 0 :(得分:0)

我建议去递归函数,它可以检查每个内部数组并添加[基本情况](What is a RECURSIVE Function in PHP?

这是一个例子:它可能对您有所帮助,也可能不是。

$exampleArray = array(1, 2, array(10,20,30, array(50,100)), 4);

function sum_array($array) {

   $total = 0;
   foreach ($array as $element) {
     if(is_array($element)) {
        $total += sum_array($element);
     } else {
        $total += $element;
     }
   }
  return $total;
}

答案 1 :(得分:0)

我能提出的最简单的解决方案是创建一个额外的数组,比方说$products_used

填写$rebate_by_product支票时:

$product_nr=$rebate_by_product[1]['products'][1];
if(!isset($products_used[$product_nr])){
      //Throw error here
      }
else{
    $products_used[$product_nr]=true;
}