-1

what is a quick and clean way to output a property for each object in an array. for example:

$array=[object1,object2,object3];

now I only need to get the name for each object:

object->name;

and output as a string like

name1,name2,name3

I don't like using for loop, I guess there is a quick way in php, please advise. thanks.

bingjie2680
  • 7,643
  • 8
  • 45
  • 72
  • "I don't like using for loop" => too localized – Gordon Mar 13 '12 at 10:30
  • 1
    possible duplicate of [Extracting a property from an array of objects](http://stackoverflow.com/questions/1118994/php-extracting-a-property-from-an-array-of-objects) – Gordon Mar 13 '12 at 10:32
  • possible duplicate of [How to get object property from each object in an array](http://stackoverflow.com/questions/6462150/how-to-get-object-property-from-each-object-in-an-array) – Gordon Mar 13 '12 at 10:33

4 Answers4

4

Use array_map to project your array of objects into an array of property values; after that implode can make you a nice little string of values:

$array = [ /* ... */ ];
echo implode(',', array_map(function($el) { return $el->name; }, $array));
Jon
  • 428,835
  • 81
  • 738
  • 806
  • Nice alternative, but how is that going to be any faster than a loop? – Steven De Groote Mar 13 '12 at 10:28
  • 1
    @StevenDeGroote: First of all, being faster was not mentioned in the question. That said, although I certainly don't know if it's going to be faster or not, I can definitely see how it *could* be faster: `array_map` is directly implemented in C while `foreach` is implemented as part of the PHP VM which should (in general) be much slower. On the other hand I would expect `foreach` to be heavily optimized since it's so common, hence I couldn't say without measuring. – Jon Mar 13 '12 at 10:31
  • this is what i am looking for, just one line of code.thanks. – bingjie2680 Mar 13 '12 at 10:40
  • @bingjie2680 just because its one line of code doesnt make it better. you can write your foreach into one line of code, too. One-liners are **not** some sort of l33t $k1ll. You are supposed to write readable code. The above could easily be formatted to span three to five lines of code to improve readability. – Gordon Mar 13 '12 at 10:42
  • @bingjie2680: As others have said; this is not "better" because it's one line, or one statement. It *could* be better because it's side-effect-free and chainable, but don't lose the forest for the trees. – Jon Mar 13 '12 at 10:53
  • thanks for your feedback, guys, I am using this in a view file, so one line of code is more readable in my case. :) – bingjie2680 Mar 13 '12 at 11:18
1

Well, you can with a foreach loop:

 foreach ($array as $value) {
      $value->name;
 }

If you don't know how many elements are in your array, I don't know how you could do this without any sort of loop.

Steven De Groote
  • 2,187
  • 5
  • 32
  • 52
1

My goodness! What's wrong with a loop? It's one of the most basic programming constructs.

Sure, you could use a closure instead:

array_walk($array, function($item) {
   echo $item->name . ",";
});

or:

echo implode(",", array_map(function($item) {
   return $item->name;
}, $array));

But is that really easier to comprehend than:

foreach($array as $item) {
  echo $item->name . ",";
}

Now please get over the fact you will need loops in programming. Lots of them!

Evert
  • 93,428
  • 18
  • 118
  • 189
0

You mean like

$str = $array[0]->name . ',' .
$array[1]->name . ',' .
$array[2]->name;

If you want it to be dnymic, well, you have to iterate:

$str = '';
foreach($array AS $object){
  $str .= ',' . $object->name;
}
Sgoettschkes
  • 13,141
  • 5
  • 60
  • 78