I’m banging my head trying to get my mind around this. I have worked with arrays before but I seem to be struggling with them in PHP. Trust me I’ve looked at questions here and other places so I apologize upfront for how basic this question probably is. What I’m trying to do is make an array of an array???
basically, I’m trying to make a session global variable so that it stores an array and that can be continually added to.
$_SESSION['school'] = array('events' => array());
example of the events array would be:
events['name'] = 'Homecoming';
events['date'] = 'December 15, 2012';
events['cost'] = 18.00;
events['attending'] = array();
events['attending'][$i] = 'John'
The use then would be something be
echo $_SESSION['school'][$i][events]['name'];
echo $_SESSION['school'][$i][events]['date'];
echo $_SESSION['school'][$i][events]['cost'];
…
I’ve tried creating it as mentioned above and populating like this (again an array of events array)
$_SESSION['school'] = array('events' => array());
$_SESSION['school'][$i][events]['name'] = 'Homecoming';
$_SESSION['school'][$i][events]['date'] = 'December 15, 2012';
$_SESSION['school'][$i][events]['cost'] = 18.00;
$_SESSION['school'][$i][events]['people'] = array('jane', 'john', 'bill');
This doesn’t seem to work because I can tell the count is going up as it is being populated. I’ve tried a few other ways but for some reason I haven’t been able to figure it out. Most examples I’ve come across don’t appear to be similar to what I’m trying do. Any help would be greatly appreciated.
——–UPDATED QUESTION BASED ON FEEDBACK——–
I think I’m very close to understanding you’re updated logic and I tried updating my code but I’m running into the following issue. I have an initialization php script which sets $_SESSION[‘school’] = array(array());. This doesn’t get populated until later and at one point I do a check on $_SESSION[‘school’] with count. It always returns 1 on the intial check even though nothing was added. However, I assume its because it holds one empty array. So it makes me question the structure.
Since I’m a visual person this is what I think I need but I’m not sure how to construct it.
//would be an 1 dimensional array that empty until a school is added later on in code.
$_SESSION['school'] = array();
it’s elements would hold a multidimensional array with elements
['name'] = 'Johnson High';
['address'] = '121 elm';
['city'] = 'san jose';
['state'] = 'California';
['events'] = array();
[‘events’] is empty until events are added but here are some items it would contain
['events']['name'] = 'homecoming';
['events']['date'] = 'December 15, 2012';
['events']['cost'] = '18.00';
in the end the $_SESSION[‘school’] could hold 1 to many schools that I could loop through.
Now to my misunderstanding of danL updated logic, I don’t see how name and events would have the same index? When I look at the code I get a sense that the the $_SESSION[‘school’] would look like this
$_SESSION['school']{ 'name' => 'Johnson High',
'events' => array('basketball', 'soccer', 'football'),
'name' => 'West Forsyth High',
'events' => array('basketball', 'soccer', 'football')};
If that’s the case then each element is at a different index. I must be missing something fundamental here. I’m continually trying to read up and make the connection to what I’m doing wrong. I think I’m just looking at the problem from the wrong angle.
Ok, so based on the help of danL (thanks so much). I’ve figured out what I need to do. So in my initialization script I set,
after further processing I create a events array to store the info in.
the last two lines could probably be joined into one line.
then I add the event array to the school array as,
I don’t know how I get spun around so easily with associative arrays but thanks for all the help.