Hi;

I'm getting more serious about getting skilled with use of arrays. Here's something I'm not clear on:

Say I've established a couple of arrays:

$arr_1 = array("foo" => "bar", 12 => true);

$arr_2 = array("First item", 2, "Third item");

THEN I combine them into a two-dimensional array like so:

$m_arr = array($arr_1, $arr_2);

Up to this point everything works fine: If I echo $m_arr[1][2], I get the value "Third item" just like I expect.

Later on in the sequence of my script, say I want to add another element into $arr_1. So I do this:

$arr_1[]=("Surprise!"); //key in [] will be 13 because 12 has been used as first number/integer key

When I call to echo $m_arr[1][13] i get nothing.

In order for this to work I've found I have to:

unset ($m_arr); and then re-create it.

$m_arr = array($arr_1, $arr_2);

is this a necessary step to get the new value into the multidimensional array or is there a more direct way?

Thanks

    Yes, since $m_arr cant automagically get the value of $arr_1

      I think I get it now. I found that I can make this work and avoid the unset/recreate step if I add the value directly into $m_arr like so:

      $m_arr[0][]=("Surprise!");

      So this means that $m_arr contains COPIES of the original $arr_1 and $arr_2 contents rather than some sort of pointers to the original array definitions?

        So this means that $m_arr contains COPIES of the original $arr_1 and $arr_2 contents rather than some sort of pointers to the original array definitions?

        That's correct. Your manipulation can be done directly in the $m_arr array's $arr_1 and $arr_2 like you have. They are precisely that... copies of the originals.

          Thank you both;

          Now, on to the "foreach ($array_x AS $array_y)" stuff that befuddles the heck out of me...

            I think the best way to look at that is to think about what it is actually saying:

            foreach($array AS $k => $v){
                echo $k." - ".$v."<br />";
            }
            

            Basically put, this says that while in the foreach loop, that for each index in the array, we will refer to the index as $k and the corresponding value in the pair as $v.

            In this simple loop, it outputs as key - value, line breaks, and repeats it through the entire loop.

            so, if your array looked like this:
            $array = ("color" => "blue",
            "food" => "chocolate",
            "drink" => "coffee",
            "person" => "mom");

            Then the above simple loop would output the following:

            color - blue
            food - chocolate
            drink - coffee
            person - mom

            It's not that difficult to conceptualize on a single dimension array. It can become trickier when you work with multidimensional arrays, but the premise is the same. Once you get the hang of the single arrays, you should be able to transition into the multis pretty easily.

              6 days later

              Maybe I should start this as another thread, but since we've got the ball rolling here....

              With your help I've got the basics of looping through and getting values from a two-dimensional array, but I've still got one little pesky problem:

              Say I've got a bunch of students, they have been tested at various time of the year. Sometimes a kid may have been absent for a test and missed it, so there's no test score data.

              I get my data for all phases with a query something like:

              "SELECT ID, TESTPHASE, SCORE FROM DATATABLE"

              The results all get nicely packed into a multidimensional array called $SCORES.

              Later on, I want to unpack this array into variables that I can use in a jpgraph (I dearly love jpgraph!).

              This nested set of FOREACH loops gets me pretty close to where I want:

              FOREACH ($SCORES AS $TESTPHASE => $KIDSCORES) {
              	ECHO "$TESTPHASE : $KIDSCORES<BR>";
              		FOREACH ($KIDSCORES AS $ID => $SCORE) {
              		ECHO $SCORES[$TESTPHASE][$ID]."<BR>";
              			}
              	}

              But, for those cases where I have a student who missed the test during a particular phase, I need to insert a NULL for the "cell" where the data would be missing. I've figured out how to do this for multi-record insert queries, but have not quite figured it out for this situation.

              I suppose I need to insert some sort of if/else operation in my nested loops? Is the "isset" command involved?

              Thanks...

                Write a Reply...