简体   繁体   中英

Need to echo only first element of an associative array in foreach php

I have a long associative array but I am showing a small part of it here:

Array
(
[0] => Array
    (
        [0] => Array
            (
                [RoomType] => Array
                    (
                [@roomTypeId] => 1927848
                [RoomImages] => Array
                    (
                       [RoomImage] => Array
                            (
                                [0] => Array
                                    (
                                        [url] => http://media.expedia.com/hotels/2000000/1620000/1611500/1611477/1611477_106_s.jpg
                                    )
                            )

                    )
                    )    
            )
    )


        [1] => Array
            (

                [RoomType] => Array
                    (
                        [@roomTypeId] => 1927848
                    [RoomImages] => Array
                         (
                    [RoomImage] => Array
                            (
                                [0] => Array
                                    (
                                        [url] => http://media.expedia.com/hotels/2000000/1620000/1611500/1611477/1611477_106_s.jpg
                                    )


                            )

                    )

            )

    )

[1] => Array
    (
        [0] => Array
            (
                [RoomType] => Array
                    (
                [@roomTypeId] => 1927848
                [RoomImages] => Array
                    (
                       [RoomImage] => Array
                            (
                                [0] => Array
                                    (
                                        [url] => http://media.expedia.com/hotels/2000000/1620000/1611500/1611477/1611477_106_s.jpg
                                    )
                            )

                    )
                    )    
            )
    )


        [1] => Array
            (

                [RoomType] => Array
                    (
                        [@roomTypeId] => 1927848
                    [RoomImages] => Array
                         (
                    [RoomImage] => Array
                            (
                                [0] => Array
                                    (
                                        [url] => http://media.expedia.com/hotels/2000000/1620000/1611500/1611477/1611477_106_s.jpg
                                    )


                            )

                    )

            )

    )

)

What i need here is that to print only [url] once for every element . Like we have two element in [0] array and two elements in [1] array but i need to print the url for them only once for each array .

I am not sure if it is written in good form. please correct it if there are mistakes .

attached an image for example .

Thanks for help. But i don't need the array of url . But i want to print them by skipping the others .You can see below :

foreach($group as $k => $v){
    foreach($v as $key => $hotelRoom){
        <tbody class="<?php echo $iscollapse; ?> searchpage<?php echo $page; ?>">
            <tr>
               <td>
                    <div>
                        <?php if(array_key_exists('RoomImages',$hotelRoom)) { ?>
                                 <img src="<?php echo setHotelImage($hotelRoom['RoomImages']['RoomImage']['0']['url'],'_b','_s'); ?>" class="img-responsive">
                        <?php } ?>
                    </div>
               </td>
            <td>
        </tr>
     </tbody>
     }
}

I am printing the array like this already where $group is the array a writtenabove. In this example, foreach you can see that it is printing that image for every element. so i want to skip that for other elements and print only for first.

Try like this..

echo $array[0][0]['RoomImages']['RoomImage'][0]['url'];//image url from first array indexed at 0
echo $array[1][0]['RoomImages']['RoomImage'][0]['url'];//image from second array indexed at 1

OR make array of url's as below:

foreach($array as $key=>$value){
$urls[]=$arr[$key][0]['RoomImages']['RoomImage'][0]['url']
}
print_r($urls);//outputs url from each array only once.. in your case two urls

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM