Solved

Understanding multidimensional arrays

Posted on 2007-04-04
6
231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 27

Expert Comment

by:Cornelia Yoder
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:
Cornelia Yoder 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

751 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