0

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";
    }           
}
2
  • 1
    how about $arr_row_name[$x][$slot] = "Open"; Commented Aug 28, 2013 at 17:51
  • They say you may get unexpected results if you modify an array while traversing it. It might be better to make a copy of the array. Then, while traversing the original, you alter the copy. Then you overwrite the original with the copy. Commented Aug 22, 2014 at 21:32

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Ah. I know I was missing something. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.