Joe Doran
Joe Doran

Reputation: 1

PHP Modify an Array within an Array

I created an Array and put the Array inside another Array. I would like to now modify the Array, but the code never does modify it.
Here is the Create Part:

$arr_row_name = array();
for($nrow=0;$nrow < $numRows;$nrow++)
{
    $arr_slot_name = array();
    for($nsp=0;$nsp < $numServProvider + 1;$nsp++)
    {
        $arr_slot_name[] = "Closed";
    }
    //Add Slot to the row.......
    $arr_row_name[] = $arr_slot_name;
}

Here is the part where I try to access the array and modify it.

$arr_row_length = count($arr_row_name);
for($x=0;$x<$arr_row_length;$x++)
{
    $arr_slot_name = $arr_row_name[$x];
    $arr_slot_length = count($arr_slot_name);
    for($slot=0;$slot<$arr_slot_length;$slot++)
    {
        $arr_slot_name[$slot] = "Open";
    }           
}

Upvotes: 0

Views: 71

Answers (1)

keithhatfield
keithhatfield

Reputation: 3273

In your second bit of code, change:

$arr_slot_name = $arr_row_name[$x];

to:

$arr_slot_name = &$arr_row_name[$x];

The way that you have it, you are making a copy of $arr_row_name[$x] into $arr_slot_name ... in the second option, you are assigning it by reference and will be able to change the original ...

Upvotes: 1

Related Questions