显示嵌套数组PHP

时间:2011-12-21 07:20:25

标签: php arrays cakephp

我正在使用cakePHP 2.2并想知道如何在这个nest数组中输出信息。

我正在尝试在[Job] =>中显示数据数组& [儿童] =>阵列。我提供了我目前的cakePHP代码,可以提供帮助。

数组输出

[4] => Array
    (
        [Job] => Array
            (
                [id] => 20
                [parent_id] => 0
                [rght] => 6
                [lft] => 1
                [client_id] => tasd
                [contact] => asdf
                [email] => sdf
                [address] => 
                [lat] => 
                [long] => 
                [user_id] => 1
                [request_type_id] => Electrical
                [date_start] => 0000-00-00 00:00:00
                [date_end] => 0000-00-00 00:00:00
                [date_complete] => 0000-00-00 00:00:00
                [date_closed] => 0000-00-00 00:00:00
                [status] => open
                [brief_desc] => aasdf
                [desc] => asdfasdf
                [cost_est] => 3434.00
                [cost_actual] => 
                [created] => 2011-12-18 20:39:24
                [modified] => 2011-12-18 20:39:24
            )


        [Children] => Array
            (
                [0] => Array
                    (
                        [id] => 21
                        [parent_id] => 20
                        [rght] => 3
                        [lft] => 2
                        [client_id] => TEST3333
                        [brief_desc] => testsdf
                        [desc] => asdfasdfasdf
                        [cost_est] => 3434.00
                        [cost_actual] => 
                        [created] => 2011-12-18 20:42:13
                        [modified] => 2011-12-18 20:42:13
                    )

                [1] => Array
                    (
                        [id] => 22
                        [parent_id] => 20
                        [rght] => 5
                        [lft] => 4
                        [client_id] => TEST666666    
                        [brief_desc] => testsdf
                        [desc] => asdfasdfasdf
                        [cost_est] => 3434.00
                        [cost_actual] => 
                        [created] => 2011-12-18 20:42:43
                        [modified] => 2011-12-18 20:42:43
                    )

当前cakePHP代码:

  <?php

foreach ($jobs as $job): ?>

  <li><a href="/jobs/view/<?php echo h($job['Job']['id']); ?>">
  <h3>J<?php echo h($job['Job']['id']); ?> - <?php echo h($job['Job']['brief_desc']); ?     ></h3>
  <p><?php echo h($job['Job']['desc']); ?></p>
  <a href="/jobs/edit/<?php echo h($job['Job']['id']); ?>"></a>     

  </a>

  <?php




  ?>
  </li>



<?php endforeach; ?>

2 个答案:

答案 0 :(得分:5)

尝试:

foreach($jobs as $job) {
  echo $job['Job']['id'];
  foreach($job['Children'] as $child) {
     echo $child['id'];
  }
}

答案 1 :(得分:0)

阵 (     [0] =&gt; stdClass对象         (             [category_id] =&gt; 2             [category_name] =&gt;家             [parent_id] =&gt;         )

[1] => stdClass Object
    (
        [category_id] => 3
        [category_name] => Clothing
        [parent_id] => Array
            (
                [0] => stdClass Object
                    (
                        [category_id] => 8
                        [category_name] => dreass
                        [parent_id] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [category_id] => 10
                                        [category_name] => new
                                        [parent_id] => Array
                                            (
                                                [0] => stdClass Object
                                                    (
                                                        [category_id] => 11
                                                        [category_name] => nnnn
                                                        [parent_id] => 
                                                    )

                                            )

                                    )

                            )

                    )

                [1] => stdClass Object
                    (
                        [category_id] => 9
                        [category_name] => apint
                        [parent_id] => 
                    )

                [2] => stdClass Object
                    (
                        [category_id] => 13
                        [category_name] => T-shirt
                        [parent_id] => 
                    )

            )

    )

[2] => stdClass Object
    (
        [category_id] => 4
        [category_name] => Beauty
        [parent_id] => 
    )

[3] => stdClass Object
    (
        [category_id] => 5
        [category_name] => Gift Cards 
        [parent_id] => 
    )

[4] => stdClass Object
    (
        [category_id] => 6
        [category_name] => Toys
        [parent_id] => Array
            (
                [0] => stdClass Object
                    (
                        [category_id] => 12
                        [category_name] => toy1
                        [parent_id] => 
                    )

            )

    )

[5] => stdClass Object
    (
        [category_id] => 7
        [category_name] => Sports
        [parent_id] => 
    )