Solved

# two dimetional arrays from query

Posted on 2006-05-26
154 Views
Would you explain two dimetional arrays to me? I know how to make simple one dimentional array from query. But I cannot understand well two dimentional arrays made from query. If someone explain it to me with breaking each steps into pieces, I greatfully appricate it.
portal123
0
Question by:portal123

LVL 40

Expert Comment

A 1 dimensional array is a list.

e.g.

<?php
\$a_1d_array = array(1,2,3,4,5,6,7,8,9,10);
?>

A 1 dimensional array is like a table (excel or DB).

e.g.

<?php
\$a_2d_array = array(
array(1,2,3,4,5,6,7,8,9,10),
array(2,4,6,8,10,12,14,16,18,20),
);
?>

A function like mysql_fetch_arry() will get 1 row of a result set from a query and return it as a 1d array.

If you wanted to add this 1d array to an array of rows (effectively cloning the result set), then you would use code like ...

\$a_my_2d_array[] = \$a_db_row;

This says, simplistically, add the \$a_db_row as a new element to the \$a_my_2s_array variable.

This constructs an 2d array primarily sorted by rows from the DB.

Remember DBs do NOT use arrays (normally). You need to talk in terms of rows.

A PHP variable can be multi-dimensional. Though I think upon reflection PHP actually uses nested 1d arrays (an element in an array can be an array).

e.g.
<?php
\$a_many_array = array
(
array
(
array(1,2,3),
array(2,3,4),
array(5,6,7),
array(1,2,3),
),
array
(
array(1,2,3),
array(2,3,4),
array(5,6,7),
array(1,2,3),
),
array
(
array(1,2,3),
array(2,3,4),
array(5,6,7),
array(1,2,3),
),
array
(
array(1,2,3),
array(2,3,4),
array(5,6,7),
array(1,2,3),
),
array
(
array(1,2,3),
array(2,3,4),
array(5,6,7),
array(1,2,3),
),
array(1,2,3),
array(2,3,4),
array(5,6,7),
array(1,2,3),
);
?>

You don't have to populate the entire matrix either. Spare arrays are the norm with PHP.

What else do you want to know?
0

Author Comment

Thanks your explanation. Whats function can i usually use to apply two dimentional arrays into  an actual shape?? If you do not mind, would you please make a simple table from two dimetional arrays for me??

portal123
0

LVL 40

Accepted Solution

Something along these lines maybe?

<?php
\$s_SQL = 'SELECT col1, col2, col3 FROM table';
\$r_results = xxxx_query(\$s_SQL, \$r_conn);
\$a_array = array();
while (False !== (\$a_row = xxxx_fetch_assoc(\$r_results)))
{
\$a_array[] = \$a_row;
}
?>

The xxxx is dependent upon your DB (mysql, mssql, etc).

But the principle is to get a lot of rows (1 row is a 1D array) into an array of rows (2D array ordered by row number).

0

LVL 40

Expert Comment

Thank you.
0

## Featured Post

This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…