Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.