关于PHP数组 赋值的问题?
$ss=array(array("one"=>"1","two"=>"2","three"=>"3"),array("one"=>"2","two"=>"3","thre...
$ss = array(
array("one"=>"1","two"=>"2","three"=>"3")
,array("one"=>"2","two"=>"3","three"=>"4")
);
foreach ($ss as $k) {
if($k['two'] == "3"){
$k['two'] = 1000;
}
}
print_r($ss);exit;
结果是:
Array
(
[0] => Array
(
[one] => 1
[two] => 2
[three] => 3
)
[1] => Array
(
[one] => 2
[two] => 3
[three] => 4
)
)
但是我想要的结果是:
Array
(
[0] => Array
(
[one] => 1
[two] => 2
[three] => 3
)
[1] => Array
(
[one] => 2
[two] => 1000
[three] => 4
)
)
就是这个字段被我改为 [two] => 1000 其他的都不变,有什么办法吗?我试了一晚头都晕了.
我想通过循环得到结果,而不是直接在数组里面直接改. 展开
array("one"=>"1","two"=>"2","three"=>"3")
,array("one"=>"2","two"=>"3","three"=>"4")
);
foreach ($ss as $k) {
if($k['two'] == "3"){
$k['two'] = 1000;
}
}
print_r($ss);exit;
结果是:
Array
(
[0] => Array
(
[one] => 1
[two] => 2
[three] => 3
)
[1] => Array
(
[one] => 2
[two] => 3
[three] => 4
)
)
但是我想要的结果是:
Array
(
[0] => Array
(
[one] => 1
[two] => 2
[three] => 3
)
[1] => Array
(
[one] => 2
[two] => 1000
[three] => 4
)
)
就是这个字段被我改为 [two] => 1000 其他的都不变,有什么办法吗?我试了一晚头都晕了.
我想通过循环得到结果,而不是直接在数组里面直接改. 展开
展开全部
foreach($arr as $k=>$v){
//这个的意思是圆老 循环数组$arr,把当前循环的下标(键名)赋值给$k,当前循环的数组元素赋值给$v
//然后你改变的是$v的值,这就类似于
// $a = 1;
// $b = $a;
// $a = 2;
// echo $b;
// 你改变了$a的值,但$b还是原来的1;
}
//所以你这个可以这样修改
foreach ($ss as $k=>$v) {
if($v['two'] == "3"){
$ss[$k]['two'] = 1000;
橘凳升 //或者
//$v['two'粗或] = 1000;
//$ss[$k] = $v;
}
}
print_r($ss);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你改的是变量不是引用,只需要在foreach ($ss as $k) 中改为foreach ($ss as &$k) 加个"&"的符号就可以了,很好改的.
或者直接吵隐修改肢山,没必要便升饥厅利数组,$ss[1]["two"] = 1000;直接赋值就可以了,没必要那么麻烦
或者直接吵隐修改肢山,没必要便升饥厅利数组,$ss[1]["two"] = 1000;直接赋值就可以了,没必要那么麻烦
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你赋值的是$k,而不是$ss,
$ss = array(
array("渣羡坦one"=>"1","two"=>"2","three"=>"3"如桐),
array("one"=>"2","two"=>派行"3","three"=>"4")
);
foreach($ss as $k=>$v){
if($v['two'] == 3){
$ss[$k]['two'] = 1000;
}
}
print_r($ss);
这样就是你要的结果
$ss = array(
array("渣羡坦one"=>"1","two"=>"2","three"=>"3"如桐),
array("one"=>"2","two"=>派行"3","three"=>"4")
);
foreach($ss as $k=>$v){
if($v['two'] == 3){
$ss[$k]['two'] = 1000;
}
}
print_r($ss);
这样就是你要的结果
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询