I l @ ve RuBoard |
Multidimensional arrays ”the PHP equivalent of a Perl hash ”are both simple and complicated at the same time. This is another topic you may want to delve into once your overall comfort level has improved. The theory behind a multidimensional array is that you can create an array with even more information than a standard array can by incorporating other arrays for values instead of just strings and numbers . For example: $Array1 = array("apples", "bananas", "oranges"); $Array2 = array("steaks", "hamburgers", "pork chops"); $List = array("fruits"=>$Array1, "meats"=>$Array2, "other"=>"peanuts", "cash" => 30.00); This array, $List, now consists of one string ( peanuts ), one floating-point number ( 30.00 ), and two arrays ( fruits and meats ). Pointing to an element within an array within an array is tricky. The key (pardon the pun) is to continue adding indices as necessary. So in our example, bananas is at $List["fruits"][1]. First, you point to the element (in this case, an array) in the $List array, by using ["fruits"]. Then, you point to the element within that array based upon its position ”the second item so you use the index [1]. Tip You can create a multidimensional array in one statement by using a series of nested array() calls (instead of through several steps as in this example), but I wouldn't recommend it because it is all too easy to make syntactical errors as a statement becomes more and more nested. Once you do have the interest and necessity to use multidimensional arrays, start by assigning each individual array, as you have here, and then putting them into another array rather than trying to program with nested array() statements. Tip Attempting to print a value from a multidimensional array can be complicated as well. Versions of PHP prior to 4 will not let you print such an element directly. In PHP 4, you can use curly braces to print from multidimensional arrays within a string: print ("The value I want to print is { $Array[index1] [index2]} .");. But print ("The value I want to print is $Array [index1][index2]."); will not work in any version of PHP. |
I l @ ve RuBoard |