Jump to content

How to add a new element to a 2D array ?


tmyonline

Recommended Posts

Hi guys:

 

I have a 2D associative array in a foreach loop like this:

 

foreach (boolean expression) {

  $myArray = array (

    "element_1" => array("x1", "y1", "z1")

  );

}

 

Everytime through the loop, I want to add the "element_i" associated with values (xi, yi, and zi) to the $myArray.  At the moment, what happens is that the $myArray does not expand because the element_1 and (x1, y1, and z1) are being overwritten by the new values.  Consequently, when the loop finishes, the $myArray is the same as it was.  I want it to be such that, after 2 iterations, it would add two rows like this:

 

foreach (boolean expression) {

  $myArray = array (

    "element_1" => array("x1", "y1", "z1"),

    "element_2" => array("x2", "y2", "z2"),

    "element_3" => array("x3", "y3", "z3")

  );

}

 

instead of overwriting the element_1 and (x1, y1, z1).  What is your recommendation ?  Thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.