Home > Backend Development > PHP Tutorial > 用for给数组赋值

用for给数组赋值

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-23 13:58:24
Original
1248 people have browsed it


$tests=array(id => "910003", params => array("param1" => '', "param2" => '', "param3" => ''));for ($i = 0; $i <= count($tests["params"]); $i++) {   }
Copy after login


如何用上述方法修改param1,param2,param3值?


回复讨论(解决方案)

$i <= count($tests["params"])
应为
$i < count($tests["params"])

$tests = array('id' => "910003", 'params' => array("param1" => '', "param2" => '', "param3" => ''));foreach($tests["params"] as $k=>&$t) $t = $k;print_r($tests);
Copy after login
Array(    [id] => 910003    [params] => Array        (            [param1] => param1            [param2] => param2            [param3] => param3        ))
Copy after login

$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);
Copy after login
Array(    [id] => 910003    [params] => Array        (            [param1] => 1            [param2] => 2            [param3] => 3        ))
Copy after login

谢谢!,如果这个参数名是没有规律的(如: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']);
Copy after login

谢谢!,如果这个参数名是没有规律的(如:param_23、paramAbc……),那有如何实现


如果没有规律,只能换一种办法了
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template