1

My debug($planDetails) output is as below. Why can't I access the array elements in a standard way like:

    foreach ($planDetails as $planRow) :
        echo $planRow['Plan']['Applicant']['name'];
    endforeach;

I get Undefined index: Plan error?

I also tried: echo $planRow['Plan'][]['name'];

Array
(
[PlanDetail] => Array
        (
            [id] => 54
            [name] => BasicOne
        )


[Plan] => Array
        (
            [0] => Array
                (
                    [id] => 255
                    [monthly_cost] => 20.23
                    [age_id] => 14
                    [applicant_id] => 8
                    [plan_detail_id] => 54
                    [Age] => Array
                        (
                            [id] => 14
                            [name] => 18-64
                        )

                    [Applicant] => Array
                        (
                            [id] => 8
                            [name] => Subscriber +2
                        )
               )
    [2] => Array
               (
                   [id] => 254
                   [monthly_cost] => 15.50
                   [age_id] => 14
                      [applicant_id] => 27
                [plan_detail_id] => 54
                   [Age] => Array
                       (
                           [id] => 14
                           [name] => 18-64
                       )

                   [Applicant] => Array
                       (
                           [id] => 27
                           [name] => Subscriber + 1
                       )

               )
)

2 Answers 2

3
foreach ($planDetails['Plan'] as $plan) {
    echo $plan['Applicant']['name'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Duh! Thanks. I will accept your answer when the time limit is over (about 8 mins).
2

You're missing a level in the array. You want

$planRow['Plan'][0]['Applicant']['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.