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]=>想加的东西
)
)
不知道你是增加数组的元素还是增加数组元素中的内容?
<?php$arr=array('name'=>'admin','pwd'=>'123456');
//增加数组元素
$arr['uid']=1;
//增加某个元素的内容
$arr['name']=$arr['name'].'123';
?>
温馨提示:答案为网友推荐,仅供参考
第1个回答 推荐于2018-03-04
代码参考:
$addins = array(1,2);
foreach($arr as $key => $val){
$arr[$key]['num'] =$addins[$key];
}
$addins = array(1,2);
foreach($arr as $key => $val){
$arr[$key]['num'] =$addins[$key];
}
不知道是不是你的意思
第2个回答 推荐于2016-11-05
我拿最简单的数组写了个给你参考..主要也是思路问题
$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)
}
}本回答被提问者采纳