php foreach同时遍历两个数组并输出
$cliest=Array('0'=>Array('cid'=>1,'pid'=>3,'name'=>'分类6','displayorder'=>3,'docs'=>18...
$cliest = Array (
'0' => Array ( 'cid' => 1 ,'pid' => 3 ,'name' => '分类6', 'displayorder' => 3 ,'docs' => 18 ),
'1' => Array ( 'cid' => 2 ,'pid' => 2 ,'name' => '分类2', 'displayorder' => 4 ,'docs' => 18 ) ,
'2' => Array ( 'cid' => 3 ,'pid' => 3 ,'name' => '分类3', 'displayorder' => 5 ,'docs' => 18 )
);
$dliest = Array (
'0' => Array ( 'cid' => 1 ,'pid' => 1 ,'name' => '分类8', 'displayorder' => 1 ,'docs' => 18 ),
'1' => Array ( 'cid' => 2 ,'pid' => 3 ,'name' => '分类4', 'displayorder' => 2 ,'docs' => 18 ),
'2' => Array ( 'cid' => 3 ,'pid' => 3 ,'name' => '分类1', 'displayorder' => 2 ,'docs' => 18 )
);
======== ps ==========
就用一个foreach,输出2个数组中,pid=3的数据。
在线等,谢谢给位大神,回答满意追加满分 展开
'0' => Array ( 'cid' => 1 ,'pid' => 3 ,'name' => '分类6', 'displayorder' => 3 ,'docs' => 18 ),
'1' => Array ( 'cid' => 2 ,'pid' => 2 ,'name' => '分类2', 'displayorder' => 4 ,'docs' => 18 ) ,
'2' => Array ( 'cid' => 3 ,'pid' => 3 ,'name' => '分类3', 'displayorder' => 5 ,'docs' => 18 )
);
$dliest = Array (
'0' => Array ( 'cid' => 1 ,'pid' => 1 ,'name' => '分类8', 'displayorder' => 1 ,'docs' => 18 ),
'1' => Array ( 'cid' => 2 ,'pid' => 3 ,'name' => '分类4', 'displayorder' => 2 ,'docs' => 18 ),
'2' => Array ( 'cid' => 3 ,'pid' => 3 ,'name' => '分类1', 'displayorder' => 2 ,'docs' => 18 )
);
======== ps ==========
就用一个foreach,输出2个数组中,pid=3的数据。
在线等,谢谢给位大神,回答满意追加满分 展开
2个回答
展开全部
<?php
$cliest = Array (
'0' => Array ( 'cid' => 1 ,'pid' => 3 ,'name' => '分类6', 'displayorder' => 3 ,'docs' => 18 ),
'1' => Array ( 'cid' => 2 ,'pid' => 2 ,'name' => '分类2', 'displayorder' => 4 ,'docs' => 18 ) ,
'2' => Array ( 'cid' => 3 ,'pid' => 3 ,'name' => '分类3', 'displayorder' => 5 ,'docs' => 18 )
);
$dliest = Array (
'0' => Array ( 'cid' => 1 ,'pid' => 1 ,'name' => '分类8', 'displayorder' => 1 ,'docs' => 18 ),
'1' => Array ( 'cid' => 2 ,'pid' => 3 ,'name' => '分类4', 'displayorder' => 2 ,'docs' => 18 ),
'2' => Array ( 'cid' => 3 ,'pid' => 3 ,'name' => '分类1', 'displayorder' => 2 ,'docs' => 18 )
);
$result= array_merge($cliest,$dliest);
foreach($result as $key=>$value){
if($value['pid']==3){
$data[] = $value;
}
}
print_r($data);
?>
刚搞好,我这个比一楼好啊
更多追问追答
追问
谢谢大神,代码可以。
然后有个问题忘记补充了:输出后怎样按照displayorder的值排序?
追答
<?php
$cliest = Array (
'0' => Array ( 'cid' => 1 ,'pid' => 3 ,'name' => '分类6', 'displayorder' => 3 ,'docs' => 18 ),
'1' => Array ( 'cid' => 2 ,'pid' => 2 ,'name' => '分类2', 'displayorder' => 4 ,'docs' => 18 ) ,
'2' => Array ( 'cid' => 3 ,'pid' => 3 ,'name' => '分类3', 'displayorder' => 5 ,'docs' => 18 )
);
$dliest = Array (
'0' => Array ( 'cid' => 1 ,'pid' => 1 ,'name' => '分类8', 'displayorder' => 1 ,'docs' => 18 ),
'1' => Array ( 'cid' => 2 ,'pid' => 3 ,'name' => '分类4', 'displayorder' => 2 ,'docs' => 18 ),
'2' => Array ( 'cid' => 3 ,'pid' => 3 ,'name' => '分类1', 'displayorder' => 2 ,'docs' => 18 )
);
$result= array_merge($cliest,$dliest);
foreach($result as $key=>$value){
if($value['pid']==3){
$data[] = $value;
$flag[] = $value['displayorder'];
}
}
array_multisort($flag, SORT_DESC, $data);
var_dump($data);
?>
呐?搞定啦
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2016-06-22
展开全部
如果两个数组元素个数一样多,键值一样的话:
foreach($cliest as $key => $val)
{
if($val['pid'] == 3)
var_dump($val);
if($dliest[$key]['pid'] == 3)
var_dump($dliest[$key]);
}
追问
两个数组元素不一样多,会怎样?
然后有个问题忘记补充了:输出后在按照displayorder的值排序
谢谢大神。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询