I am trying to validate an associative array pushed from my JavaScript, using the validation library.
The following code is working except it is only validating (finding the values) for the last array within the associative array, is there a way for it to work for each instance of the array as it runs in the foreach?
code:
if (!empty($JSON)) {
foreach ($JSON AS $k => $data) {
foreach ($data AS $key => $value) {
$this->form_validation->set_data($data);
if($key == 'itemCode' . $k){
$this->form_validation->set_rules($key, 'Item Code', 'required');
}
if($key == 'Desc' . $k){
$this->form_validation->set_rules($key, 'Description', 'required');
}
if($key == 'Qty' . $k){
$this->form_validation->set_rules($key, 'Quantity', 'required|numeric');
}
if($key == 'Cost' . $k){
$this->form_validation->set_rules($key, 'Cost', 'required|numeric');
}
}
//$this->form_validation->reset_validation();
}
}
array output:
[0] => Array(
[Counter0] => 0
[itemCode0] => 1
[Desc0] => 1
[Qty0] => 1
[Cost0] => 1
[Total0] => 1
)
[1] => Array(
[Counter1] => 1
[itemCode1] => 2
[Desc1] => 2
[Qty1] => 2
[Cost1] => 2
[Total1] => 4
)
[2] => Array(
[Counter2] => 2
[itemCode2] => 3
[Desc2] => 3
[Qty2] => 3
[Cost2] => 3
[Total2] => 9
)
[3] => Array(
[Counter3] => 3
[itemCode3] => 4
[Desc3] => 4
[Qty3] => 4
[Cost3] => 4
[Total3] => 16
)