7

Is there a way to convert array of objects into an array of strings using some custom mapping in PHP. Like:

$objs = array(o1, o2, o3);

...

$strings = conv($objs, function($o) -> $o->fieldXYZ);

instead of:

$objs = array(o1, o2, o3);

...

$strings = array();

foreach($objs as $obj) {
    $strings []= $obj->fieldXYZ;
}
laurent
  • 88,262
  • 77
  • 290
  • 428
Denis Kulagin
  • 8,472
  • 17
  • 60
  • 129

1 Answers1

8

I think what you are looking for is the array_map() function. For example, this should work:

$strings = array_map(function($o) {
    return $o->fieldXYZ;
}, $objs);
laurent
  • 88,262
  • 77
  • 290
  • 428