0

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

3 Answers 3

3

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']; 
}
Sign up to request clarification or add additional context in comments.

Comments

1

You only need one loop for this:

foreach($mainarray as $fieldarray){
    $body .= $fieldarray['email_label'].' - '. $fieldarray['field_name']; 
}

Comments

0

Try to use

foreach($mainarray as $fieldarray){
    $body .= $fieldarray['email_label'].' - '. $fieldarray['field_name']; 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.