dadamssg87 Posted June 3, 2011 Share Posted June 3, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/238266-help-with-foreaching-through-an-array/ Share on other sites More sharing options...
Pikachu2000 Posted June 3, 2011 Share Posted June 3, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/238266-help-with-foreaching-through-an-array/#findComment-1224431 Share on other sites More sharing options...
dadamssg87 Posted June 3, 2011 Author Share Posted June 3, 2011 ah i see. Thanks! <?php foreach($bookings as $key => $value) { echo "The id of this booking is ".$bookings[$key]['id'].".<br>"; echo "The start of this bookings is ".$bookings[$key]['start'].".<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/238266-help-with-foreaching-through-an-array/#findComment-1224432 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.