PHP Arrays: Pop an array of single-element arrays into one array.

Posted by Rob Drimmie on Stack Overflow See other posts from Stack Overflow or by Rob Drimmie
Published on 2010-05-31T16:04:02Z Indexed on 2010/05/31 16:13 UTC
Read the original article Hit count: 151

Filed under:
|

Using a proprietary framework, I am frequently finding myself in the situation where I get a resultset from the database in the following format:

array(5) {
  [0] => array(1) {
    ["id"] => int(241)
  }
  [1] => array(1) {
    ["id"] => int(2)
  }
  [2] => array(1) {
    ["id"] => int(81)
  }
  [3] => array(1) {
    ["id"] => int(560)
  }
  [4] => array(1) {
    ["id"] => int(10)
  }
}

I'd much rather have a single array of ids, such as:

array(5) {
  [0] => int(241)
  [1] => int(2)
  [2] => int(81)
  [3] => int(560)
  [4] => int(10)
}

To get there, I frequently find myself writing:

$justIds = array();
foreach( $allIds as $id ) {
  $justIds[] = $id["id"];
}

Is there a more efficient way to do this?

© Stack Overflow or respective owner

Related posts about php

Related posts about arrays