Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Nested Array Problem.

    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
    Attached Files Attached Files

  • #2
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can get the values of multidimensional arrays like so:

    PHP Code:
    $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.
    "$question = ( to() ) ? be() : ~be();"

  • #3
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmmm...

    well, i put in this line to replace them:

    Code:
    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:
    Code:
    	$temp = $note[$stringnumber][$fretnumber];
    	echo "Fret $fretnumber: $temp<br>";
    EDIT AGAIN: Okay, I have a solution that should be fine, it works like this:
    Code:
    	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:
    Code:
    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?
    Last edited by Submerged; 01-04-2006 at 05:17 PM.

  • #4
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you're getting the value of $scale with the offset from $note[$s][0] it goes inside one bracket.

    PHP Code:
    echo $scale[$note[$s][0]]; 
    "$question = ( to() ) ? be() : ~be();"

  • #5
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ah, that works. I think i got it all figured out now, thanks again


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •