1
public function include_header( $h, $h_data ) {

    // Require header file
    if (isset($h) && !empty($h)) {
        $this->header_file = 'includes/header/'.$h;
    } else { return false; }

    // Pass optional array of parameters
    if (isset($h_data) && is_array($h_data) && !empty($h_data)) {

        // Loop through array & assign keys to appropriate class members
        foreach($h_data as $key => $val) {

            if ($key == 'doctype') { $this->doctype = $val; }
            if ($key == 'title') { $this->title = $val; }
            if ($key == 'meta') {
                // The meta key is should be an array in h_data
                // so, we'll have to loop through the meta array
                // in order to parse the individual meta elements.
                foreach($key as $meta_key => $meta_val) {
                    $this->error = $meta_key;
                }

            }

        }

    } else { return false; }

}

I'm trying to loop through a multidimensional array, where I have the following variable...

$h_data['meta']['individual_element']

I'm trying to loop through the 'meta', so I can access each individual value, but I'm having trouble. Please help! Thanks in advance!

2
  • 2
    Your nested for each should walk over $val not $key Commented May 23, 2013 at 1:54
  • Is the foreach loop not working correctly or do you need help looping through multi-dimensional array? What problem is it that your having? Commented May 23, 2013 at 1:55

2 Answers 2

5
foreach($key as $meta_key => $meta_val) {
    $this->error = $meta_key;
}

... should be

foreach($val as $meta_key => $meta_val) {
    $this->error = $meta_key;
}
Sign up to request clarification or add additional context in comments.

Comments

4
 if ($key == 'meta') {
                // The meta key is should be an array in h_data
                // so, we'll have to loop through the meta array
                // in order to parse the individual meta elements.
                foreach($val as $meta_key => $meta_val) { //val not key
                    $this->error = $meta_key;
                }

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.