Question : (Simple) Creating array, appending and recreating.

Hello,

I've narrowed a bug in my code down to the way I create a new array. Please see the attached code. Essentially, I create an array ($mArray2) and once it has been populated I append the array to another array ($mArray1), although when I next try to recreate the inner array ($mArray2), I lose all the values previously assigned. How can I get around this so when I append mArray2 to mArray1, I get a copy?

Thanks,
Uni
1:
2:
3:
4:
5:
6:
7:
8:
9:
$mArray1=array();
for($i1=0;$i1<10;$i1++){
	$mArray2=array();
	for($i2=0;$i2<10;$i2++){
		$mArray2[]=new CMyClass();
		//Populate the latest mArray2 element.
	}
	$mArray1[]=$mArray2;
}

Answer : (Simple) Creating array, appending and recreating.

Try this.  The problem in your code is that array are passed by reference.
1:
2:
3:
4:
5:
6:
7:
$mArray1=array();
for($i1=0;$i1<10;$i1++){
	$mArray1[]=array();
	for($i2=0;$i2<10;$i2++){
		$mArray1[$i1][]=new CMyClass();
	}
}
Random Solutions  
 
programming4us programming4us