Solved

Understanding multidimensional arrays

Posted on 2007-04-04
6
226 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Advice and best practice  SQLite3 Database using PHP 2 46
Loop through multiple arrays 13 27
ebay devID, appID, certID, userToken 2 24
Showing random records from database 10 35
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now