Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Understanding multidimensional arrays

Posted on 2007-04-04
6
Medium Priority
?
233 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 2000 total points
ID: 18854811
Just print the array and see what it holds :)

http://us3.php.net/manual/en/function.print-r.php
0
Industry Leaders: 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

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.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

715 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