一个有关数组的问题(自我感觉有点难)
本帖最后由 mafeifan 于 2013-07-10 11:02:33 编辑 已知
$items = array(
array('dna_id'=>55,'status'=>1),
array('dna_id'=>55,'status'=>0),
array('dna_id'=>56,'status'=>1),
array('dna_id'=>58,'status'=>0),
array('dna_id'=>58,'status'=>0),
array('dna_id'=>60,'status'=>0),
array('dna_id'=>60,'status'=>0),
array('dna_id'=>60,'status'=>0),
array('dna_id'=>60,'status'=>0)
);
欲得到下面的结果,active未完成的记录,个数及id
array(
'active'=>array('count'=>3,'ids'=>array('55','58','60')),
'completed'=>array('count'=>1,'ids'=>array('56'))
);
dna_id可能有多条,当每条dna_id对应的status必须都是1或没有0才放到completed里面,否则放到active里面,通过遍历或者更简单的方法怎么得到上面的结果呢 分享到: 55,'status'=>1),array('dna_id'=>55,'status'=>0... data-pics=>
------解决方案--------------------
可以这样写$items = array(
array('dna_id'=>55,'status'=>1),
array('dna_id'=>55,'status'=>0),
array('dna_id'=>56,'status'=>1),
array('dna_id'=>58,'status'=>0),
array('dna_id'=>58,'status'=>0),
array('dna_id'=>60,'status'=>0),
array('dna_id'=>60,'status'=>0),
array('dna_id'=>60,'status'=>0),
array('dna_id'=>60,'status'=>0),
);
$res = array(
'active'=> array(
'count' => 0,
'ids' => array()
),
'completed' => array(
'count' => 0,
'ids' => array()
),
);
foreach($items as $v) {
if(!in_array($v['dna_id'], $res['active']['ids'])) $res['active']['ids'][] = $v['dna_id'];