tags:

views:

44

answers:

3

Every day , I have this pattern - an array of objects - i make a loop to traverse the array

foreach($arr as $obj){
 $arrIds[]  = $obj->Id;
 $arrNames[] = $obj->Name;
 }

I could build a function like arrayFromProperties($Array,$ProperyName) but I was wondering if you know a native php function to do this, or something similar, without having to write a new class/function for this.

+1  A: 

As far as I know, you'll have to do this by your own.

joni
A: 

Have you tried get_object_vars?

fire
A: 

There isn't. It's not very hard to write, either. Feel free to reuse or adapt the following to your liking:

function soa_of_aos($aos) 
{
  $soa = array();
  foreach ($aos as $i => $struct)       
    foreach ($struct as $field => $value) 
      if (!isset($soa[$field])) $soa[$field] = array($i => $value);
      else $soa[$field][$i] = $value;
  return $soa;
}
Victor Nicollet