-2

I have an array like this:

$a1 = array(
    array('id'=>"1","others"=>"asdhuisah"),
    array('id'=>"4","others"=>"asdhuisah"),
    array('id'=>"213","others"=>"asdhuisah")
);

and I want to save a part of it into a new array $a2.

I know I can do it using loops:

$a2 = array();
foreach($a1 as $key=>$value){
    array_push($a2,$value["id"]);
}

Is there a function that I can use to do all this at once?

Jürgen Thelen
  • 12,745
  • 7
  • 52
  • 71
v-joy
  • 25
  • 1
  • 5

3 Answers3

0

Try this:

$fun_id = function($value){

    return $value["id"];

} ;

$a2 = array_map($fun_id, $a1);
levi
  • 22,001
  • 7
  • 73
  • 74
0

You can use array_map and a lambda function:

$return_id = function($item) {
  return $item['id'];
};
$ids_array = array_map($return_id, $a1);
Chris Trahey
  • 18,202
  • 1
  • 42
  • 55
  • I am wondering if there is a buildin function witch can do this job. but thanks for your help. – v-joy Aug 06 '12 at 02:01
0

I think array_slice http://www.php.net/manual/en/function.array-slice.php can do it for you.

artragis
  • 3,677
  • 1
  • 18
  • 30