0

I've an associative array titled $rebate_by_product. This associative array is dynamic in nature means it can be very long or very short depending on data it contains. For your reference I'm showing below the small instance of it which contains three rebate entries.

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
)

You can observe from above array that I'm storing the product id's of products for each rebate entry into array format under key [products]. Now I don't want repetition of these id's irrespective of the rebate entry to which they belong. That is every rebate entry must have different product ids under key [products]. To achieve this I think each product id from each rebate entry should be compared with others. May be my approach is wrong. When you come across first match of product ids, error message should display as "Please select different products" and when not a single match found after comparing all the product ids with each other, the success message should display as "All the products are different". How to achieve this in optimum way with minimum loop iterations and by making use of ready made PHP array functions? Thanks for spending some of your time in understanding my issue. If you want any other information regarding the issue I can provide you the same. Any kind of help, suggestions, comments, answers would be highly appreciated. Waiting for your precious replies. Thanks.

  • Do I understand correct that your `array[3]` should fail because it has `products[11]` in it, while `array[2]`also has a `products[11]`? – Michel May 06 '14 at 15:44
  • @Michel:Yes you have understood my requirement perfect. Would you please provide me the detail answer for array $rebate_by_product? –  May 06 '14 at 16:48
  • I've given an suggestion below. – Michel May 06 '14 at 16:50
  • Questions asking for code must demonstrate a minimal effort in solving the actual problem, including the attempted code and the encountered issues – HamZa May 06 '14 at 18:17

2 Answers2

0

i would suggest go for recursive function where it can check each inner array and add a [base case] (What is a RECURSIVE Function in PHP?)

this is an example: it might be helpful for you or may be not.

$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;
}
Community
  • 1
  • 1
vnimbala
  • 74
  • 5
0

The simplest solution I can come up with, is to create an additional array, lets say $products_used.

When you populate your $rebate_by_product check:

$product_nr=$rebate_by_product[1]['products'][1];
if(!isset($products_used[$product_nr])){
      //Throw error here
      }
else{
    $products_used[$product_nr]=true;
}
Michel
  • 4,076
  • 4
  • 34
  • 52
  • Thanks for your help but I'm not understanding the code you wrote above. Could you please take up my array rebate_by_product and give me the precise and in detail answer? It will be of great help for me. –  May 06 '14 at 17:49
  • Before I explain, can you provide us with some cone on how you populate the array. Does it come from a database or from a form? – Michel May 07 '14 at 06:12