...

View Full Version : Nested Array Problem.



Submerged
01-04-2006, 07:44 AM
Hi, I am creating a guitar site, which is eventually going to be extremely complicated. My question deals with a smaller part though.

My goal is to have a nested array to store all the note values at each fret for each string on the fretboard. To do this, I have a nested array like this:

$note[$stringnumber[$fretnumber]]

I have this set up correctly; If I print_r($note[1]) it comes out with the correct numbers (I have numbers, 1-12 to represent the notes. I can convert them later, I'm not worrying about it for this example).

I can read a note by assigning $temp to equal $note[$stringnumber], then calling $temp[$fretnumber]. This gives me the numeric value for the note at the given fret on the given string.

However, I am hoping there is a way to do this without an intermediary variable (the $temp), with one nice step. I don't know if this is possible, but I hope it is. I've included the php file, as dumbed down as I can get it. The part I'm asking about is at the bottom, I labelled it with comments. The 2 lines I would like to combine are:

$temp = $note[$stringnumber];
echo "Fret $fretnumber: $temp[$fretnumber]<br>";

Thanks for any help :)

-Alex

Velox Letum
01-04-2006, 07:52 AM
You can get the values of multidimensional arrays like so:


$array = array(array('Foo', 'Bar'), array('Oof', 'Rab'));
echo $array[0][1]; // Returns 'Bar' (without quotes)
To access more dimensions, just add another offset (or string if its associative) to the end.

Submerged
01-04-2006, 04:46 PM
hmmm...

well, i put in this line to replace them:



echo "Fret $fretnumber: $note[$stringnumber][$fretnumber]<br>";


and the output I get is:

Fret 1: Array[1]

which repeats for each string and each fret (the 1's in the example output here do change correctly). But it doesnt seem to actually be reading the array, just saying that there is an array there.


EDIT: I can get it to work like this though:

$temp = $note[$stringnumber][$fretnumber];
echo "Fret $fretnumber: $temp<br>";

EDIT AGAIN: Okay, I have a solution that should be fine, it works like this:
echo "Fret $fretnumber: ".$note[$stringnumber][$fretnumber]."<br>";

Thanks for the help Velox :)


EDIT.... again... Okay, one more question :P I have a part where I have a very nested array, it goes like this:


echo $scale[$note][$s][0];

Where I want it to find $note[$s][0], and then plug that in for $scale[]. Right now I think its trying to take $scale[$note], then take that[$s] after that, and so on, which wouldnt work. If I echo

echo $note[$s][0];

it works fine, i think its jsut a order of operations thing. Any suggestions? :)

Velox Letum
01-04-2006, 08:18 PM
If you're getting the value of $scale with the offset from $note[$s][0] it goes inside one bracket.


echo $scale[$note[$s][0]];

Submerged
01-04-2006, 08:25 PM
ah, that works. I think i got it all figured out now, thanks again :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum