An array-related question (I feel a little difficult from my younger brother)

Source: Internet
Author: User
An array-related question (self-perception is a bit difficult) This post was last edited by mafeifan from 2013-07-10:0:33 known $ items & nbsp ;=& nbsp; array (& nbsp; & nbsp; array ('DNA _ id' = & gt; 55, 'status' is an array-related question (self-feeling a bit difficult)
This post was last edited by mafeifan at 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)    
);

To obtain the following results, the number, and id of the uncompleted active Records

array(
    'active'=>array('count'=>3,'ids'=>array('55','58','60')),
    'completed'=>array('count'=>1,'ids'=>array('56'))
);

There may be multiple DNA IDs. when the status corresponding to each DNA _id must be 1 or No 0, it will be placed in completed; otherwise, it will be put into active, how can I get the above results through traversal or a simpler method? share to: 55, 'status' => 1 ),???? Array ('DNA _ id' => 55, 'status' => 0... 'data-pics = ''>
------ Solution --------------------
Write like this
$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'];

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.