Reputation: 2207
I use a multidimensional array in a foreach loop, but i dont get the right results back.
array
$mainarray = array(
array('field_name' => 'xx',
'email_label' => 'xxxx',
'validation_type' => 'xxxxx',
'validation_msg' => 'xxxxxx'),
array('field_name' => 'xx',
'email_label' => 'xxxx',
'validation_type' => 'xxxxx',
'validation_msg' => 'xxxxxx'),
// more ....
}
foreach loop
foreach($mainarray as $fieldarray){
foreach($fieldarray as $key => $value){
$body .= $value['email_label'].' - '. $value['field_name'];
}
}
i need the value's of the key called email_label and field_name, but i dont get the right results back
Upvotes: 0
Views: 882
Reputation: 141829
You only need one loop for this:
foreach($mainarray as $fieldarray){
$body .= $fieldarray['email_label'].' - '. $fieldarray['field_name'];
}
Upvotes: 1
Reputation: 7504
Try to use
foreach($mainarray as $fieldarray){
$body .= $fieldarray['email_label'].' - '. $fieldarray['field_name'];
}
Upvotes: 0
Reputation: 33769
Since your code that appends to $body
accesses indexes of $value
, your original code was effectively written to work on a three-level array.
If your array is structured as you've posted, you don't need the inner foreach
loop.
foreach($mainarray as $fieldarray) {
$body .= $fieldarray['email_label'].' - '. $fieldarray['field_name'];
}
Upvotes: 3