Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multi-dimensional arrays - How would you explain them? 1

Status
Not open for further replies.

josel

Programmer
Oct 16, 2001
716
US
I have worked with arrays for many years. The problem is that I have used one (1) programming language and it is nothing like PHP or anything else out there ...

The arrays I've used since 1989 are one dimension (vertical). Before this I did some Clipper programming where I used multi-dimensional arrays but this many years later, I find myself in the dark.

If I am using the wrong terminology, please let me know as I do not mind being corrected ...

I want to write an application where I figure the use of arrays is the way to go. I want to load a data table to a data grid and get standard input to fill-in blanks. Here is the grid (kind of)

SKU Desc QTY
+------+-----------+------+
| | | |
+------+-----------+------+
| | | |
+------+-----------+------+
| | | |
+------+-----------+------+

The grid could have a couple of hundreds SKUs upon initial load. When the first form is submitted, the second form will display a grid only with SKUs where a QTY has been entered.

Using the language I currently use (character based / UNIX), I could write this in matter of hours (OK, you got me, a day or two including debugging) and I would have to define an array per column on the grid.

I know that PHP offers better array handling and it will be worth my putting off the project until I better understand them.

I hope this explains my objective. What do you guys/gals suggest I do?

Regards,


Jose Lerebours

PS: Any idea what is up with Search?


KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
i think your method is fine. md arrays are a bit easier but, in reality, there is nothing materially different between

Code:
$col1Array[0] = 'apple';
$col2Array[0] = 'fruit';
$col1array[1] = 'oregano';
$col2array[1] = 'herb';
and
Code:
$array[0] = array('apple', 'fruit');
$array[2] = array('oregano', 'herb');

in some ways single dimension arrays are more useful as they allow you to search them (provided you keep the array indices in sync across the columns).

to get your database recordset into a multicolumn array
Code:
$result = mysql_query("Select itemName, itemType from tableItems");
while ($row[] = mysql_fetch_assoc($result)){
//do nothing
}
your recordset will be in a multidimensional array that will look something lke this
Code:
[0]
   [itemName] -> 'apple',
   [itemType] -> 'fruit'
[1]
   [itemName] -> 'oregano',
   [itemType] -> 'herb'

you can address any element of the array in the usual way
Code:
echo $row[1]['itemName'];
and you would normally iterate through the array using foreach constructs or similar
Code:
foreach ($row as $data){
  if(is_array($data)){
    foreach ($data as $key=>$val){
      echo "$key \t $val <br/>"
    }
  } else {
      echo "$data<br/>";
  }
}
 
Excellent!

It does not appear too complicated. I see that the element name is defined by the dataset loaded to the array.

It looks like if I wanted to use a single array for the entire grid, I would have to add 'dummy' fields to my table so that these are automatically defined on the array as well. I can live with this.

Thank you so very much for your great and well detailed reply.

Regards,


Jose Lerebours


KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top