Jump to content

help with foreaching through an array


dadamssg87

Recommended Posts

I'm putting selected rows into an array with each row as its own array in that array. Here's a print_r($bookings) of what i'm trying to say. $bookings is the parent array that holds it all.

Array

(

    [1] => Array

        (

            [id] => 189

            [name] => Spans june and july

            [start] => 2011-06-30 00:01:00

            [end] => 2011-07-01 00:00:00

        )

 

    [2] => Array

        (

            [id] => 193

            [name] => Only July

            [start] => 2011-07-11 00:01:00

            [end] => 2011-07-12 00:00:00

        )

 

    [3] => Array

        (

            [id] => 194

            [name] => Clean up after Only July

            [start] => 2011-07-13 00:00:00

            [end] => 2011-07-13 00:00:00

        )

 

)

 

now i'm trying to walk through the each array thats in the parent array like so.

<?php
foreach($bookings as $key)
{

echo "The id of this booking is ".$bookings[$key]['id'].".<br>";
echo "The start of this bookings is ".$bookings[$key]['start'].".<br>";

}
?>

 

This isn't working. I'm getting an "Illegal offset type" error. Anybody know what i'm not doing right?

 

 

Link to comment
https://forums.phpfreaks.com/topic/238266-help-with-foreaching-through-an-array/
Share on other sites

If you specify only one parameter, it doesn't matter what you name it, it will return the value of the element, not the value of the key. In other words, your $key variable holds an array.

 

foreach

 

foreach( $array as $key => $value ) {} // will return both key and value

foreach( $array as $value ) {} // Is no different from:
foreach( $array as $key ) {} // does not return the value of the key

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.