Click to See Complete Forum and Search --> : Arrays?


gruetztian
08-20-2003, 06:23 AM
hi,
in a project i try to work with multidimensional Arrays like this sample below

$genre = array("Stamps","Coins");

$genre[0] = array("International","National");//Stamps

$genre[0][0] = array("Spain","Italy");//Stamps/International
$genre[0][1] = array("1990","1995");//Stamps/National

$genre[1] = array("International","National");//Coins

$genre[1][0] = array("Spain","Italy");//Coins/International
$genre[1][1] = array("1990","1995");//Coins/National

first question: is this construct possible

second question:how to read Data of first Dimension

The Data in the third dimension in this sample is:

$genre[0][0][0] = "Spain";

but if i try to get the Data of the first or second Dimension:

$genre[0] the output is -> Array

whats the secret of working with arrays???

pyro
08-20-2003, 07:48 AM
That is not how multidimensional arrays work. All values that you want to receive out of the array need to be in the last dimension of the array.

Da Warriah
08-20-2003, 07:59 AM
something like this would probably be better, tho definitely confusing depending on how many of these you wanted:

$genre = array(
array("Stamps",
array(
array("International",
array("Spain, "Italy")
),
array("National",
array("1990", "1995")
)
),

array("Coins",
array(
array("International",
array("Spain", "Italy")
),
array("National",
array("1990", "1995")
)
)
);

that way, $genre[1][1][0][1][1] is "Italy", under "Coins > International"...($genre[1][0] is "Coins", $genre[1][1][0][0] is "International")....see how confusing it is, even for a "simple" array?

and i dont know why i even bothered to do that, i just needed the practice i guess;)

AdamBrill
08-20-2003, 08:07 AM
This is the correct way for the arrays that you posted:


$genre[0] = array(array("Spain","Italy"),array("1990","1995"));//Stamps
$genre[1] = array(array("Spain","Italy"),array("1990","1995"));//Coins Or this way(which is a little easier to look at ;))$genre[0] = array();//Stamps
$genre[0][0] = array("Spain","Italy");
$genre[0][1] = array("1990","1995");
$genre[1] = array();//Coins
$genre[1][0] = array("Spain","Italy");
$genre[1][1] = array("1990","1995");
Either way, this:

echo $genre[0][0][0];

will echo "Spain" onto the page... :)

AdamBrill
08-20-2003, 09:32 AM
Or you could do it like this:$genre = array();
$genre["Stamps"] = array();
$genre["Stamps"]["International"] = array("Spain","Italy");//Stamps
$genre["Stamps"]["National"] = array("1990","1995");
$genre["Coins"] = array();
$genre["Coins"]["International"] = array("Spain","Italy");//Stamps
$genre["Coins"]["National"] = array("1990","1995");Then this:echo $genre["Stamps"]["International"][0];will write "Spain" onto the screen... I just figured you might like that one better. ;)