View Full Version : Nested Array Problem.

01-04-2006, 06: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:


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 :)


Velox Letum
01-04-2006, 06: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.

01-04-2006, 03:46 PM

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, 07: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]];

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