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
https://forums.phpfreaks.com/topic/91637-how-to-add-a-new-element-to-a-2d-array/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.