Solved

Understanding multidimensional arrays

Posted on 2007-04-04
6
223 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Router for PHP reqeusts 12 33
resizeing PHP image 2 24
PHP loop not working 4 33
How to fetch your row in php 14 13
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 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

12 Experts available now in Live!

Get 1:1 Help Now