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/>";
最后 打印的结果 有很多的重复的值,有什么好的方法实现二维数组赋值 问题呢?
//两个二维数组的格式如下,例子: 如所示,$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/>";
最后 打印的结果 有很多的重复的值,有什么好的方法实现二维数组赋值 问题呢?
相关文章推荐
- PHP MYSQL命令
- WINDOWS中设置计划任务执行PHP文件
- php使用curl的post方法字符串和数组传值的区别
- Laravel数据库操作
- PHP实现html生成pdf、html生成图片 利用wkhtmltox,wkhtmltopdf扩展
- php——composer 1、安装使用
- thinkphp小技巧
- ubuntu14.04中php-fpm的问题
- LTP算子
- php发送get、post请求的几种方法
- thinkphp模板的详细知识
- MAC安装并破解PhpStorm-9.0.2 记录(附下载链接)
- ThinkPHP 的一些知识
- 关于PHP中func_get_args()、func_get_arg()、func_num_args()的使用(PHP函数重载)
- php 通过txt文件将数据装填入数据库
- PHP数组的操作
- Thinkphp控制volist的循环次数,限制只显示部分数据
- 此文为转载,虽然写的是PHP,但是思路同样值得javaer参考!
- thinkphp模板页限制标题字数
- thinkphp getField