php数组如何增加内容
Array([0]=>Array([region_id]=>1[region_name]=>中国[parent_id]=>0)[1]=>Array([region_id]...
Array
(
[0] => Array
(
[region_id] => 1
[region_name] => 中国
[parent_id] => 0
)
[1] => Array
(
[region_id] => 2
[region_name] => 北京
[parent_id] => 1
)
)
上边是原数组 是php查询出的结果print_r($arr)出来的
想要如下效果
Array
(
[0] => Array
(
[region_id] => 1
[region_name] => 中国
[parent_id] => 0
[num]=>想加的东西
)
[1] => Array
(
[region_id] => 2
[region_name] => 北京
[parent_id] => 1
[num]=>想加的东西
)
) 展开
(
[0] => Array
(
[region_id] => 1
[region_name] => 中国
[parent_id] => 0
)
[1] => Array
(
[region_id] => 2
[region_name] => 北京
[parent_id] => 1
)
)
上边是原数组 是php查询出的结果print_r($arr)出来的
想要如下效果
Array
(
[0] => Array
(
[region_id] => 1
[region_name] => 中国
[parent_id] => 0
[num]=>想加的东西
)
[1] => Array
(
[region_id] => 2
[region_name] => 北京
[parent_id] => 1
[num]=>想加的东西
)
) 展开
3个回答
展开全部
代码参考:
$addins = array(1,2);
foreach($arr as $key => $val){
$arr[$key]['num'] =$addins[$key];
}
不知道是不是你的意思
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我拿最简单的数组写了个给你参考..主要也是思路问题
$green = array(
0=>array("name"=>"c","score"=>90),
1=>array("name"=>"a","score"=>80),
2=>array("name"=>"d","score"=>80),
3=>array("name"=>"b","score"=>70),
4=>array("name"=>"e","score"=>60),
);
//排名 1 2 2 4 5
//姓名 C A D B E
//成绩 90 80 80 70 60
foreach( $green as $k=>$v){
if ($k == 0){
$green[$k]['site']=1;
}else{
$lk = $k - 1;
if ($green[$k]['score'] != $green[$lk]['score']){
$nk=$k +1;
$green[$k]['site']= $nk;
}else{
$green[$k]['site']=$k;
}
}
}
下面是结果
array(5) {
[0]=>
array(3) {
["name"]=> string(1) "c"
["score"]=> int(90)
["site"]=> int(1)
}
[1]=>
array(3) {
["name"]=> string(1) "a"
["score"]=> int(80)
["site"]=> int(2)
}
[2]=>
array(3) {
["name"]=> string(1) "d"
["score"]=> int(80)
["site"]=> int(2)
}
[3]=>
array(3) {
["name"]=>string(1) "b"
["score"]=>int(70)
["site"]=>int(4)
}
[4]=>
array(3) {
["name"]=>string(1) "e"
["score"]=> int(60)
["site"]=>int(5)
}
}
$green = array(
0=>array("name"=>"c","score"=>90),
1=>array("name"=>"a","score"=>80),
2=>array("name"=>"d","score"=>80),
3=>array("name"=>"b","score"=>70),
4=>array("name"=>"e","score"=>60),
);
//排名 1 2 2 4 5
//姓名 C A D B E
//成绩 90 80 80 70 60
foreach( $green as $k=>$v){
if ($k == 0){
$green[$k]['site']=1;
}else{
$lk = $k - 1;
if ($green[$k]['score'] != $green[$lk]['score']){
$nk=$k +1;
$green[$k]['site']= $nk;
}else{
$green[$k]['site']=$k;
}
}
}
下面是结果
array(5) {
[0]=>
array(3) {
["name"]=> string(1) "c"
["score"]=> int(90)
["site"]=> int(1)
}
[1]=>
array(3) {
["name"]=> string(1) "a"
["score"]=> int(80)
["site"]=> int(2)
}
[2]=>
array(3) {
["name"]=> string(1) "d"
["score"]=> int(80)
["site"]=> int(2)
}
[3]=>
array(3) {
["name"]=>string(1) "b"
["score"]=>int(70)
["site"]=>int(4)
}
[4]=>
array(3) {
["name"]=>string(1) "e"
["score"]=> int(60)
["site"]=>int(5)
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |