2009-10-21 php copy array

我的code 很簡單, 做test driven development 寫test的時候
pass in 的data 很多時候都要重用
只是其中一個field 的值修改, 其餘的都不變
所以:

<?php
function testSubmit(){
//init. data
$data = array(
   
'user' => 'abc',
   
'password' => 'def',
);

//copy array
$test = array_merge($data);
//change value
$test['user'] = '';
//test
$this->assertTrue(foo($test));

//copy again
$test = array_merge($data);
//change value
unset($test['user']);
//test
$this->assertTrue(foo($test));
}
?>

那我便可以方便的修改pass in 的data (只修改一個地方)

重點是, array 的複製要使用 array_merge()...

Google