如果你在这个循环迭代中发现任何其他错误,请告诉我。
我有一个标题为$form_data
的数组,如下所示:
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
)
我已经编写了一个函数来验证数组条目。得到的参数为$form_data
阵列和$error_msgs
阵列。其他对象是在类的构造函数中创建的。所以,请忽略这些东西,并考虑我在foreach循环中得到的错误。
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');
}
}
}
}
}
}
}
在上面的代码中,我添加了一个注释来指示行号。我得到警告的地方。
$products
似乎没有定义。你的意思是:
foreach( $value as $k => $v) {
再看一遍,它应该是:
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) {
在循环到达的地方:
[1] => Array
(
[pack] => 10
您正在检查is_array()
,但您需要进入嵌套的级别,以获得键 products
。
你把事情弄得更复杂了。
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.
}
}
}