php如何操作数组遍历追加数据
规则:用数组1中的id匹配数组2中的uid如果数组2中的uid和数组1中的uid相同的话那就让数组2中的信息显示在数组1中uid相同的下面(独立的一个数组内)数组1每个子...
规则:用数组1中的id匹配数组2中的uid
如果数组2中的uid和数组1中的uid相同的话
那就让数组2中的信息显示在数组1中uid相同的下面(独立的一个数组内)
数组1每个子数组下面都要显示3条数组2的信息
uid和uid相同的优先显示
uid和uid相同大于3条的话就只显示3条
小于3条时用其余的从小到达显示补全
求大神们解答
数组1
$arr1 = array(
array('uid'=>1,'times'=>'2014-02-25','counts'=>'19'),
array('uid'=>2,'times'=>'2014-02-26','counts'=>'17'),
array('uid'=>3,'times'=>'2014-02-27','counts'=>'6')
);
数组2
$arr2 = array(
array('id'=>1,'uid'=>1,'times'=>'2014-02-25','counts'=>'19'),
array('id'=>2,'uid'=>1,'times'=>'2014-02-26','counts'=>'17'),
array('id'=>3,'uid'=>1,'times'=>'2014-02-27','counts'=>'16'),
array('id'=>4,'uid'=>3,'times'=>'2014-02-28','counts'=>'18'),
array('id'=>5,'uid'=>1,'times'=>'2014-02-28','counts'=>'15'),
array('id'=>6,'uid'=>4,'times'=>'2014-02-28','counts'=>'14'),
array('id'=>7,('uid'=>4,'times'=>'2014-02-28','counts'=>'13'),
array('id'=>8,'uid'=>1,'times'=>'2014-02-28','counts'=>'12'),
array('id'=>9,'uid'=>1,'times'=>'2014-02-28','counts'=>'11'),
array('id'=>10,'uid'=>3,'times'=>'2014-02-28','counts'=>'10')
);
最后生成一个这样的数组
Array
(
[0] => Array
(
[arr1] => Array
(
array('id'=>1,'times'=>'2014-02-25','counts'=>'19'),
)
[arr2] => Array
(
array('id'=>1,'uid'=>1,'times'=>'2014-02-25','counts'=>'19'),
array('id'=>2,'uid'=>1,'times'=>'2014-02-26','counts'=>'17'),
array('id'=>3,'uid'=>1,'times'=>'2014-02-27','counts'=>'16'),
)
)
[1] => Array
(
[arr1] => Array
(
array('id'=>2,'times'=>'2014-02-26','counts'=>'17'),
)
[arr2] => Array
(
array('id'=>5,'uid'=>1,'times'=>'2014-02-28','counts'=>'15'),
array('id'=>8,'uid'=>1,'times'=>'2014-02-28','counts'=>'12'),
array('id'=>9,'uid'=>1,'times'=>'2014-02-28','counts'=>'11'),
)
)
[2] => Array
(
[arr1] => Array
(
array('id'=>3,'times'=>'2014-02-27','counts'=>'6')
)
[arr2] => Array
(
array('id'=>4,'uid'=>3,'times'=>'2014-02-28','counts'=>'18'),
array('id'=>10,'uid'=>3,'times'=>'2014-02-28','counts'=>'10'),
array('id'=>6,'uid'=>4,'times'=>'2014-02-28','counts'=>'14'),
)
)
) 展开
如果数组2中的uid和数组1中的uid相同的话
那就让数组2中的信息显示在数组1中uid相同的下面(独立的一个数组内)
数组1每个子数组下面都要显示3条数组2的信息
uid和uid相同的优先显示
uid和uid相同大于3条的话就只显示3条
小于3条时用其余的从小到达显示补全
求大神们解答
数组1
$arr1 = array(
array('uid'=>1,'times'=>'2014-02-25','counts'=>'19'),
array('uid'=>2,'times'=>'2014-02-26','counts'=>'17'),
array('uid'=>3,'times'=>'2014-02-27','counts'=>'6')
);
数组2
$arr2 = array(
array('id'=>1,'uid'=>1,'times'=>'2014-02-25','counts'=>'19'),
array('id'=>2,'uid'=>1,'times'=>'2014-02-26','counts'=>'17'),
array('id'=>3,'uid'=>1,'times'=>'2014-02-27','counts'=>'16'),
array('id'=>4,'uid'=>3,'times'=>'2014-02-28','counts'=>'18'),
array('id'=>5,'uid'=>1,'times'=>'2014-02-28','counts'=>'15'),
array('id'=>6,'uid'=>4,'times'=>'2014-02-28','counts'=>'14'),
array('id'=>7,('uid'=>4,'times'=>'2014-02-28','counts'=>'13'),
array('id'=>8,'uid'=>1,'times'=>'2014-02-28','counts'=>'12'),
array('id'=>9,'uid'=>1,'times'=>'2014-02-28','counts'=>'11'),
array('id'=>10,'uid'=>3,'times'=>'2014-02-28','counts'=>'10')
);
最后生成一个这样的数组
Array
(
[0] => Array
(
[arr1] => Array
(
array('id'=>1,'times'=>'2014-02-25','counts'=>'19'),
)
[arr2] => Array
(
array('id'=>1,'uid'=>1,'times'=>'2014-02-25','counts'=>'19'),
array('id'=>2,'uid'=>1,'times'=>'2014-02-26','counts'=>'17'),
array('id'=>3,'uid'=>1,'times'=>'2014-02-27','counts'=>'16'),
)
)
[1] => Array
(
[arr1] => Array
(
array('id'=>2,'times'=>'2014-02-26','counts'=>'17'),
)
[arr2] => Array
(
array('id'=>5,'uid'=>1,'times'=>'2014-02-28','counts'=>'15'),
array('id'=>8,'uid'=>1,'times'=>'2014-02-28','counts'=>'12'),
array('id'=>9,'uid'=>1,'times'=>'2014-02-28','counts'=>'11'),
)
)
[2] => Array
(
[arr1] => Array
(
array('id'=>3,'times'=>'2014-02-27','counts'=>'6')
)
[arr2] => Array
(
array('id'=>4,'uid'=>3,'times'=>'2014-02-28','counts'=>'18'),
array('id'=>10,'uid'=>3,'times'=>'2014-02-28','counts'=>'10'),
array('id'=>6,'uid'=>4,'times'=>'2014-02-28','counts'=>'14'),
)
)
) 展开
1个回答
2016-12-16 · 知道合伙人互联网行家
关注
展开全部
可以遍历两次$a, 第一次取出所有的父级栏目,放入$newarr中,第二次将子栏目放入对应
的父级栏目数组中, 最后使用$newarr就行
不过这是建立在只有二级栏目的基础上,如果有三级栏目的话就还要麻烦些了
$newarr = array();
foreach ($a as $key => $value)
{
if($value['fID'] == 0)
{
$cid = $value['cID'];
$newarr[$cid] = $value;
unset($a[$key]);
}
}
foreach ($a as $key => $value)
{
if($value['fID'] != 0)
{
$fid = $value['fID'];
$newarr[$fid]['zilanmu'][] = $value;
}
}
的父级栏目数组中, 最后使用$newarr就行
不过这是建立在只有二级栏目的基础上,如果有三级栏目的话就还要麻烦些了
$newarr = array();
foreach ($a as $key => $value)
{
if($value['fID'] == 0)
{
$cid = $value['cID'];
$newarr[$cid] = $value;
unset($a[$key]);
}
}
foreach ($a as $key => $value)
{
if($value['fID'] != 0)
{
$fid = $value['fID'];
$newarr[$fid]['zilanmu'][] = $value;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询