Why the warning is coming as "Warning: Invalid argument supplied for foreach() in /var/www/smart-rebate-web/model/RebateByProduct.php on line 53"? [duplicate]

StackOverflow https://stackoverflow.com/questions/23512304

Question

I've an array titled $form_data as follows:

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

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

            )

        [2] => Array
            (
                [pack] => 50
                [quantity] => 60
                [volume] => 70
                [units] => 10
                [amount] => 80
                [rebate_start_date] => 2014-05-06
                [rebate_expiry_date] => 2014-05-10
                [applicable_states] => Array
                    (
                        [0] => 14
                        [1] => 15
                        [2] => 16
                    )

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

            )

        [3] => Array
            (
                [pack] => 100
                [quantity] => 200
                [volume] => 300
                [units] => 7
                [amount] => 400
                [rebate_start_date] => 2014-05-21
                [rebate_expiry_date] => 2014-05-30
                [applicable_states] => Array
                    (
                        [0] => 26
                        [1] => 33
                        [2] => 42
                    )

                [rebate_total_count] => 9999
                [products] => Array
                    (
                        [1] => 9
                        [2] => 8
                    )

            )

        [multiselect] => 42
    )

I've written a function to validate the array entries. It is getting the parameters as $form_data array and $error_msgs array. Other objects are created in constructor of a class. So, please ignore those things and consider the error I'm getting in foreach loop.

function ValidateRebateByProductFormData($form_data, $errors_msgs) {    
    if(!$this->mValidator->validate($form_data['company_id'], "required", "true"))
       $this->mValidator->push_error($errors_msgs['company_id'], 'company_id');
$product_ids = array(); 
    // loop them, if its an array, loop inside it again
    /*for outer array $form_data*/
    foreach($form_data as $index => $element) {
      /*check for each key of array $form_data whether it's an array or not*/
      if(is_array($element)) {
        /*fo inner array having index [1], [2], [3],...*/
        foreach($element as $key => $value) {
          /*check for each key of inner array [1], [2], [3],.. whether it's an array or not*/
          if(is_array($value)) {
            if($key == 'products') {
              foreach($products as $k => $v) {//This is line 53 where I'm getting above warning
                if(!$this->mValidator->validate($v, "required", "true"))
                  $this->mValidator->push_error($errors_msgs['product_id'], 'product_id');
                  //$product_ids = array_merge($product_ids, $value);
                }
            } else {
              //Validations for pack
              if(!$this->mValidator->validate($element['pack'], "numeric", "true"))
                $this->mValidator->push_error($errors_msgs['pack_invalid'], 'pack');

              //Validations for quantity
              if(!$this->mValidator->validate($element['quantity'], "required", "true"))
                $this->mValidator->push_error($errors_msgs['quantity'], 'quantity');
              elseif(!$this->mValidator->validate($element['quantity'], "numeric", "true"))
                $this->mValidator->push_error($errors_msgs['quantity_invalid'], 'quantity');

              //Validations for volume
              if(!$this->mValidator->validate($element['volume'], "required", "true"))
                $this->mValidator->push_error($errors_msgs['volume'], 'volume');
              elseif(!$this->mValidator->validate($element['volume'], "numeric", "true"))
                $this->mValidator->push_error($errors_msgs['volume_invalid'], 'volume');

              //Validations for units
              if(!$this->mValidator->validate($element['units'], "required", "true"))
                $this->mValidator->push_error($errors_msgs['units'], 'units');

              //Validations for amount
              if(!$this->mValidator->validate($element['amount'], "required", "true"))
                $this->mValidator->push_error($errors_msgs['amount'], 'amount');
              elseif(!$this->mValidator->validate($element['amount'], "numeric", "true"))
                $this->mValidator->push_error($errors_msgs['amount_invalid'], 'amount');

              //Validations for rebate start date
              if(!$this->mValidator->validate($element['rebate_start_date'], "required", "true"))
                $this->mValidator->push_error($errors_msgs['rebate_start_date'], 'rebate_start_date');
              elseif(!$this->mValidator->validate($element['rebate_start_date'], "date", "true"))
                $this->mValidator->push_error($errors_msgs['rebate_start_date_invalid'], 'rebate_start_date');

              //Validations for rebate expiry date
              if(!$this->mValidator->validate($element['rebate_expiry_date'], "required", "true"))
                $this->mValidator->push_error($errors_msgs['rebate_expiry_date'], 'rebate_expiry_date');
              elseif(!$this->mValidator->validate($element['rebate_expiry_date'], "date", "true"))
                $this->mValidator->push_error($errors_msgs['rebate_expiry_date_invalid'], 'rebate_expiry_date');

              //Validation for rebate start date and rebate expiry date
              if(change_date_format_to_db($element['rebate_expiry_date'], 'Y-m-d')<change_date_format_to_db($element['rebate_start_date'], 'Y-m-d'))
                $this->mValidator->push_error($errors_msgs['rebate_exp_date'], 'rebate_start_date');

              if(clean($element['rebate_total_count'])!="") {
                if(!$this->mValidator->validate($element['rebate_total_count'], "integer", "true"))
                  $this->mValidator->push_error($errors_msgs['rebate_total_count_invalid'], 'rebate_total_count');
              }            
            }           
          }
        } 
      }
    }
  }

In above code I've added a comment to indicate the line no.53 where I'm getting warning. If you find any other bugs in this loop iterations please let me know.

Était-ce utile?

La solution

$products doesn't appear to be defined. Did you mean:

foreach( $value as $k => $v) {

Autres conseils

Looking again, it would rather be:

foreach($element as $key => $value) {
          /*check for each key of inner array [1], [2], [3],.. whether it's an array or not*/
          if(is_array($value)) {
             foreach($value as $key1=>$value1){
                if($key1 == 'products') {
                  foreach($value['products'] as $k => $v) {

At the point where the loop reaches:

[1] => Array
            (
                [pack] => 10

You are checking with is_array() but you need to get into the nested level to have the key products.

Your making it more complicated than it is.

foreach($form_data as $key => $value) {

    if(is_array($value)) {

        foreach($value['products'] as $productkey => $productValue) {

            echo $productKey; // This is the key.
            echo $productValue; // This is the actual product ID.

        }

    }

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top