Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

this is my array in php $hotels

Array
(
    [0] => Array
        (
        [hotel_name] => Name
        [info] => info
        [rooms] => Array
            (
                [0] => Array
                    (
                        [room_name] => name
                        [beds] => 2
                        [boards] => Array
                            (
                                [board_id] => 1
                                [price] =>200.00
                            )
                    )
                )
        )
)

how can i get board_id and price i have tryed few foreach loops but cant get the result

   foreach($hotels as $row)
    {
       foreach($row as $k)
       {
          foreach($k as $l){
             echo $l['board_id'];
             echo $l['price'];
          }
       }
}

this code didnt work, and its kinda stupid

share|improve this question

1 Answer

up vote 3 down vote accepted

This is the way to iterate on this array:

foreach($hotels as $row)
{
       foreach($row['rooms'] as $k)
       {
             echo $k['boards']['board_id'];
             echo $k['boards']['price'];
       }
}

You want to iterate on the hotels and the rooms (the ones with numeric indexes), because those seem to be the "collections" in this case. The other arrays only hold and group properties.

share|improve this answer
This is the case of iterating. But it would be great if you can tell a short cut to get the $k['boards'] array only. Or I would say, I need to get the boards with board_id = 1. – Jithin Apr 3 at 16:53
@Jithin If you need the one with board_id = 1, it can be done, just browse the PHP manual, the Array Functions section, you will find handy tools there if you need something nicer than foreach. – bažmegakapa Apr 3 at 19:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.