/    Sign up×
Community /Pin to ProfileBookmark

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???

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@pyroAug 20.2003 — 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.
Copy linkTweet thisAlerts:
@Da_WarriahAug 20.2003 — 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?
Copy linkTweet thisAlerts:
@AdamBrillAug 20.2003 — This is the correct way for the arrays that you posted:

[code=php]
$genre[0] = array(array("Spain","Italy"),array("1990","1995"));//Stamps
$genre[1] = array(array("Spain","Italy"),array("1990","1995"));//Coins [/code]
Or this way(which is a little easier to look at ?)[code=php]$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");[/code]

Either way, this:

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

will echo "Spain" onto the page... ?
Copy linkTweet thisAlerts:
@AdamBrillAug 20.2003 — Or you could do it like this:[code=php]$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");[/code]
Then this:[code=php]echo $genre["Stamps"]["International"][0];[/code]will write "Spain" onto the screen... I just figured you might like that one better. ?
×

Success!

Help @gruetztian spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...