0

I am using cakePHP 2.2 and wondering how I can output information within this nest array.

I am trying to display the data within [Job] => Array & [Children] => Array. I have provided my current cakePHP code which may assist.

Array output

[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
                    )

Current cakePHP code:

  <?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; ?>
5
  • could you be clear, what are you trying to achieve..? Commented Dec 21, 2011 at 7:23
  • Sorry I have updated my post to make it more clear. Commented Dec 21, 2011 at 7:28
  • is that foreach loop not working..? Commented Dec 21, 2011 at 7:30
  • That is working perfect at displaying data from the [Job], I can't get it from [Children] Commented Dec 21, 2011 at 7:34
  • Thanks Sudhir, I tried something similar to this but it didn't work. All is good now. Commented Dec 21, 2011 at 7:41

2 Answers 2

5

Try:

foreach($jobs as $job) {
  echo $job['Job']['id'];
  foreach($job['Children'] as $child) {
     echo $child['id'];
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

If it is a recursive structure that can have multiple levels of nesting, then you might want to look into the TreeHelper and the options around this.
0

Array ( [0] => stdClass Object ( [category_id] => 2 [category_name] => Home [parent_id] => )

[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] => 
    )

)

1 Comment

You will be of even more help if you explain you answer. See here for reference: stackoverflow.com/help/how-to-answer

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.