访问特定的array()元素

时间:2011-07-15 22:51:10

标签: php arrays cakephp foreach

我的调试($ planDetails)输出如下。为什么我不能以标准方式访问数组元素,如:

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

我得到未定义索引:计划错误?

我也尝试过: 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 个答案:

答案 0 :(得分:3)

foreach ($planDetails['Plan'] as $plan) {
    echo $plan['Applicant']['name'];
}

答案 1 :(得分:2)

你错过了数组中的一个级别。你想要

$planRow['Plan'][0]['Applicant']['name'];