Forum Moderators: coopster

Message Too Old, No Replies

3D associative array looping

3D associative array looping

         

sbrocks

9:25 pm on Oct 7, 2009 (gmt 0)

10+ Year Member



I've built my array of locations, month option groups and then the days in that month. In the end I want a dropdown that looks like-
Location name (from id in the array)
October
[key] 10/15/2009 [text] thursday the 15th 4 - 6pm
[key] 10/16/2009 [text] blah blah
November
blah blah
Location name (from id in the array) ---etc.

I'm stepping through each loop but I'm stuck on the first one.

My array is this-


$_VOLDATE = array
(
126 => array
('October' => array
('10/19/2009' => 'Monday, October 19:4pm - 8pm',
'10/20/2009' => 'Tuesday, October 20:4pm - 8pm',
'10/21/2009' => 'Wednesday, October 21:4pm - 8pm',
'10/22/2009' => 'Thursday, October 15:4pm - 8pm',),
'November' => array
('11/2/2009' => 'Monday, November 2:4pm - 8pm',
'11/3/2009' => 'Tuesday, November 3:4pm - 8pm',
'11/4/2009' => 'Wednesday, November 4:4pm - 8pm',
'11/5/2009' => 'Thursday, November 5:4pm - 8pm',),
),

220 => array
('November' => array
('11/16/2009' => 'Monday, November 16:4:30pm - 8:30pm',
'11/17/2009' => 'Tuesday, November 17:4:30pm - 8:30pm',
'11/19/2009' => 'Thursday, November 19:4:30pm - 8:30pm',
'11/30/2009' => 'Monday, November 30:4:30pm - 8:30pm',),
'December' => array
('12/1/2009' => 'Tuesday, December 1:4:30pm - 8:30pm',
'12/2/2009' => 'Wednesday, December 2:4:30pm - 8:30pm',
'12/3/2009' => 'Thursday, December 3:4:30pm - 8:30pm',),
)
);

When I run this code -


foreach ($_VOLDATE as $ids => $id)
{
echo "id ".print_r($id)."<br>";
}

I get this back -


Array ( [October] => Array ( [10/19/2009] => Monday, October 19: 4pm - 8pm [10/20/2009] => Tuesday, October 20: 4pm - 8pm [10/21/2009] => Wednesday, October 21: 4pm - 8pm [10/22/2009] => Thursday, October 15: 4pm - 8pm ) [November] => Array ( [11/2/2009] => Monday, November 2: 4pm - 8pm [11/3/2009] => Tuesday, November 3: 4pm - 8pm [11/4/2009] => Wednesday, November 4: 4pm - 8pm [11/5/2009] => Thursday, November 5: 4pm - 8pm ) ) id 1
Array ( [November] => Array ( [11/16/2009] => Monday, November 16: 4:30pm - 8:30pm [11/17/2009] => Tuesday, November 17: 4:30pm - 8:30pm [11/19/2009] => Thursday, November 19: 4:30pm - 8:30pm [11/30/2009] => Monday, November 30: 4:30pm - 8:30pm ) [December] => Array ( [12/1/2009] => Tuesday, December 1: 4:30pm - 8:30pm [12/2/2009] => Wednesday, December 2: 4:30pm - 8:30pm [12/3/2009] => Thursday, December 3: 4:30pm - 8:30pm ) ) id 1

Shouldn't I be getting back -
Array ( [126] => Array ([October => Array => Monday ...etc?

What have I missed?
Thanks for any help, I've killed myself just getting this far!
ps- I do need to type in the day options, they don't follow any logic that would allow me to use date functions.

sned

9:58 pm on Oct 7, 2009 (gmt 0)

10+ Year Member



I'd say what you are getting back looks correct.

In your foreach statement, the $ids variable will be the numeric key before each month, in your case, '126' for October. The print_r function is looking at the array that key 126 points to, so it's won't print out 126.

If you wanted to see the whole thing, you could just do a print_r($_VOLDATE).

Does that help?

edit:
Also useful is doing something like:
echo '<pre>';
print_r($_VOLDATE);
echo '</pre>';

TheMadScientist

10:08 pm on Oct 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Run this and you should see the difference:
The first $value ($ids) of $key ($id) 126 is what you are seeing in the foreach. It's correct, because you are asking for the value of the first key, but not echoing the $key ($ids) anywhere... If you want to know what the $key ($ids) is, you will have to echo $ids.

<?php

$_VOLDATE = array
(
126 => array
('October' => array
('10/19/2009' => 'Monday, October 19:4pm - 8pm',
'10/20/2009' => 'Tuesday, October 20:4pm - 8pm',
'10/21/2009' => 'Wednesday, October 21:4pm - 8pm',
'10/22/2009' => 'Thursday, October 15:4pm - 8pm',),
'November' => array
('11/2/2009' => 'Monday, November 2:4pm - 8pm',
'11/3/2009' => 'Tuesday, November 3:4pm - 8pm',
'11/4/2009' => 'Wednesday, November 4:4pm - 8pm',
'11/5/2009' => 'Thursday, November 5:4pm - 8pm',)
)
);

print_r($_VOLDATE);

echo "<br /><br />";

foreach ($_VOLDATE as $ids => $id)
{
echo "id ".print_r($id)."<br>";
}
?>

sbrocks

10:34 pm on Oct 7, 2009 (gmt 0)

10+ Year Member



That does help. I understand why I'm seeing what's IN 126 and 220 instead of seeing '126' and '220'.
So that part actually works the way I wanted it to. However when I try to build the entire loop I get an error:

--Warning: Invalid argument supplied for foreach()-- on the foreach ($months....) line.

I want to end up with 2 drop downs (more in the real-life project). This is the loop-


foreach ($_VOLDATE as $locs => $loc)
{
echo("<select name='sel_date' id='$loc';' >");
echo '<option></option>';
foreach ($months as $month => $dates)
{
echo "<optgroup label=\"$month\">";
foreach ($dates as $date => $text)
{
echo "<option value='$date'>$text</option>";
}
echo '</optgroup>';
}
}

The way I have it mapped out looks logical to me, so I can't
see where I've gone wrong.
Thanks for helping, I've pretty much hit the wall here.

sned

10:39 pm on Oct 7, 2009 (gmt 0)

10+ Year Member



In your loop, I don't see where the $months and $dates variables are coming from.

I'd say your loop should look something like:


foreach($_VOLDATE as $locs => $loc){
...
foreach($loc as $month=>$dates){
...
foreach($dates as $day=>$time){
...
}
}
}

TheMadScientist

10:42 pm on Oct 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just to add to what sned said, in my experience the error you are seeing usually comes from trying to run a foreach on a variable rather than an array.

sbrocks

2:49 pm on Oct 8, 2009 (gmt 0)

10+ Year Member



Thank you. It's making much more sense to me now.
So if I want a foreach that will create a drop-down for each location do I need to add another dimension to the array? That's what it's looking like to me, I won't have time to try again until later - but is that the right idea?
Sen and MadScientist-- I really appreciate your input. It's saving me hours of hair-pulling.