您的位置:首页 > 编程语言 > PHP开发

php 二维数组赋值的 问题,求大神指教

2015-10-09 00:00 711 查看
摘要: php, 二维数组,二维数组循环中赋值

//两个二维数组的格式如下,例子: 如所示,$userList中的user_id 与 $getList中的SellerID 相等,则将$getList 中的Count的值,添加到$userList 中, 如果不相等则为空字符串,目标结果如$newArr,该如何实现呢?

我自己 试着写的代码 未能实现、

$getList = array(

0=>array(

),

1=>array(

),

2=>array(

'SellerID'=>'32300308',

'Count'=>'1'

),

3=>array(

'SellerID'=>'34818910',

'Count'=>'2'

)

);

$userList = array(

0=>array(

'username'=>'zhangsan',

'user_id'=>'32300308',

'city'=>'北京',

'Count'=>'1'

),

1=>array(

'username'=>'zhangsan',

'user_id'=>'34818910',

'city'=>'南京',

'Count'=>'2'

),

2=>array(

'username'=>'zhangsan',

'user_id'=>'34818911',

'city'=>'天津',

'Count'=>''

)

);

/*

$newArr = array(

0=>array(

'username'=>'zhangsan',

'user_id'=>'32300308',

'city'=>'北京',

'Count'=>'1'

),

1=>array(

'username'=>'zhangsan',

'user_id'=>'34818910',

'city'=>'南京',

'Count'=>'2'

),

2=>array(

'username'=>'zhangsan',

'user_id'=>'34818911',

'city'=>'天津',

'Count'=>''

)

);

*/

代码:

$newArr = array();

if(is_array($userList) && count($userList)>0)

{

foreach ($userList as $k=>$v)

{

//$userIds[]= $v['user_id'];

foreach ($getList as $k=>&$val)

{

//$sellerIds = $val['SellerID'];//接口id

//var_dump($sellerIds);

//var_dump($v['user_id']);

if($v['user_id'] == $val['SellerID'])

{

if($v['count'] == '')

{

$v['count'] = $val['Count'];

}

$newArr[] = $v;

}

else

{

$v['count'] = '';

$newArr[]= $v;

}

}

}

}

print_r($newArr);

echo "<hr/>";

最后 打印的结果 有很多的重复的值,有什么好的方法实现二维数组赋值 问题呢?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: