MKC
MKC

Reputation: 188

Looping Multidimensional Arrays with FOREACH

I know similar questions have been asked before but I can't get the solutions to work for me as I really struggle with using a foreach loop for multidimensional arrays, still can't quite get my head around it.

The structure of my array $bookings is as follows:

Array ( [#BOOKED#] => Array ( [0] => Array ( [id] => 1 [user_id] => 2 [class_id] => 3 [class_date] => 2014-03-03 00:00:00 [manual_booking] => 1 [booked_date] => 2014-02-26 00:00:00 [cancelled_date] => )
              [#RESERVE#] => Array ( [0] => Array ( [id] => 2 [user_id] => 2 [class_id] => 2 [class_date] => 2014-03-04 00:00:00 [manual_booking] => 1 [booked_date] => 0000-00-00 00:00:00 [cancelled_date] => )

This is what I've attempted, where am I going wrong? Any help would be grateful received, as would any links to tutorials that explain foreach looping multidimensional arrays in really basic terms, everything I've found is either lacking detail or clear examples with explanations of what each of the parameters does. Many thanks!

foreach ($bookings as $status => $record) {
    echo $record['user_id'];
    echo '<br>';
}

Upvotes: 0

Views: 153

Answers (3)

AbraCadaver
AbraCadaver

Reputation: 78984

You almost had it:

foreach ($bookings as $record) {
    echo $record['#BOOKED#']['user_id'];
    echo '<br>';
    echo $record['#RESERVE#']['user_id'];
    echo '<br>';
}

Or split into two separate foreach depending on how you want to display it:

foreach ($bookings['#BOOKED#'] as $record) {
    echo $record['user_id'];
    echo '<br>';
}
foreach ($bookings['#RESERVE#'] as $record) {
    echo $record['user_id'];
    echo '<br>';
}

Depending on how often you use this and if you have other similar arrays you could use a function and just pass in $bookings['#RESERVE#'] for example.

Upvotes: 1

jesus g_force Harris
jesus g_force Harris

Reputation: 516

I would think of a multi-dimensional array as a library. In a library you have different areas to segregate the topics - like the top layer of an array. Then you have different book cases that categorise each topic - like the 2nd level of an array. Then a different shelf for (let's say authors) - like the 3rd level of an array. It is a very neat way to store and find data. Your multi-dimensional array (from what you've written) appears like this:

Top Level:

#BOOKED#
#RESERVE#

2nd Level: (I'm not personally understanding why this level exists)

0

3rd Level:

id
user_id
class_id
class_date
manual_booking
booked_date
cancelled_date

To separate the top level of the array you could use a condition statement like so:

foreach ($bookings as $status => $record) {
    if($status=="#BOOKED#") {
        // i.e. do something for booked array
        echo $record[0]['user_id'];
    } else {
        // i.e. do something for reserved array
        echo $record[0]['user_id'];
    }
}

Think about getting rid of that 2nd level though - it doesn't seem necessary unless you have more than just the zero value there.

Upvotes: 1

hsz
hsz

Reputation: 152206

Just try with:

foreach ($bookings as $status => $records) {
    foreach ($records as $record) {
        echo $record['user_id'];
        echo '<br>';
    }
}

Upvotes: 1

Related Questions