Rumah >pembangunan bahagian belakang >tutorial php >用for给数组赋值
$tests=array(id => "910003", params => array("param1" => '', "param2" => '', "param3" => ''));for ($i = 0; $i <= count($tests["params"]); $i++) { }
$i 应为
$i
$tests = array('id' => "910003", 'params' => array("param1" => '', "param2" => '', "param3" => ''));foreach($tests["params"] as $k=>&$t) $t = $k;print_r($tests);
Array( [id] => 910003 [params] => Array ( [param1] => param1 [param2] => param2 [param3] => param3 ))
$tests = array('id' => "910003", 'params' => array("param1" => '', "param2" => '', "param3" => ''));for($i=1; $i<=count($tests["params"]); $i++) { $tests["params"]['param'.$i] = $i;}print_r($tests);
Array( [id] => 910003 [params] => Array ( [param1] => 1 [param2] => 2 [param3] => 3 ))
谢谢!,如果这个参数名是没有规律的(如:param_23、paramAbc……),那有如何实现
不是给了你两个方案了吗?
哦,明白了!谢谢!
$tests=array('id' => "910003", 'params' => array( "param1" => '', "param2" => '', "param3" => '' ));for ($i = 1; $i<=count($tests["params"]); $i++) { $tests['params']['param'.$i]=$i;}print_r($tests['params']);
谢谢!,如果这个参数名是没有规律的(如:param_23、paramAbc……),那有如何实现