Solved

Understanding multidimensional arrays

Posted on 2007-04-04
6
227 Views
Last Modified: 2008-03-06
I need some help understanding multidimensional arrays, still haven't gotten a full grasp on this variable type. Let's say that I have a query:
$query = select id, date, time_start, time_end, cycle from press_schedule;
$res = mysql_query($query);
while($myrow=mysql_fetch_row($res))
{
$id = $myrow[0];
$db_weekday_start = $myrow[1];
$db_time_start = $myrow[2];
$db_time_end = $myrow[3];
$db_cycle = $myrow[4];
id[$db_weekday_start][$db_time_start][$db_cycle] = $myrow[0];
}

If the database contains only one row with the following info:
id = 1
date = 2007-01-01
time_start = '02:00'
time_end = '03:00'
cycle = 0

What would this array hold?
0
Comment
Question by:horalia
  • 4
  • 2
6 Comments
 
LVL 27

Expert Comment

by:yodercm
ID: 18854752
A far better way to access the database info is to use the field name as the array key.  I'm not even sure if numeric indices work, but I wouldn't use them anyway.

$query = select id, date, time_start, time_end, cycle from press_schedule;
$res = mysql_query($query);
while($myrow=mysql_fetch_row($res))
{
$id = $myrow["id"];
$db_weekday_start = $myrow["date"];
$db_time_start = $myrow["time_start"];
$db_time_end = $myrow["time_end"];
$db_cycle = $myrow["cycle"];
}


Now you can use the variables directly any way you need.
0
 

Author Comment

by:horalia
ID: 18854779
Oh, I do that too, but my question is, what would the array that I proposed in my question contain? I found this code on HotScripts that uses arrays extensively to construct a weekly scheduling calendar, and it works really good, but I would like to understand how arrays work so I can understand the logic behind the code. Thanks!
0
 
LVL 27

Accepted Solution

by:
yodercm earned 500 total points
ID: 18854811
Just print the array and see what it holds :)

http://us3.php.net/manual/en/function.print-r.php
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:horalia
ID: 18854870
OK, I'll try...
0
 

Author Comment

by:horalia
ID: 18854947
I followed your instructions, and I don't understand one printout. This is the array:
$id[$weekday][$time][$db_cycle]

This is what I get:
111111111

It doesn't make sense, but the code is certainly working. How is the array getting these values?
0
 

Author Comment

by:horalia
ID: 18854958
Never mind my last question, the statement is in a while loop...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question