3

I have a simple object (or array) like this...

stdClass Object ( [people] => Array ( [0] => stdClass Object ( [name] => 'John', [age] => 50, ) [0] => stdClass Object ( [name] => 'Martin', [age] => 47, ) ) 

And I can easily loop through it using foreach like this

foreach ($people as $person) { echo $person->name . '<br>'; } 

But I would somehow like to have a shorter way to echo all names with something like this...

print_each($people->name) 

And it would do exactly the same thing with only 1 short line of code as my 3 lines of foreach code did.

Is there a function like this or how would we go about creating a function like that?

3
  • 1
    why? readability should never be underestimated. if you must, write a function/method. Commented Sep 12, 2018 at 14:12
  • For testing/debugging purpuses. How to write a function like that? Commented Sep 12, 2018 at 14:13
  • put the foreach in a function? Commented Sep 12, 2018 at 14:38

7 Answers 7

6

You can use array_column and implode.

echo implode("<br>\n", array_column($arr, "name")); 
Sign up to request clarification or add additional context in comments.

Comments

5

Probably array_map is the closest to what you want.

array_map(function($person) { echo $person->name; }, $people); 

2 Comments

stil too long, can we write a shorter function?
Sure! How about array_map(function($p) { echo $p->name; }, $people); ;-)
4

Shortest way and not that ugly would be to add a __toString Method to the Person Object like this:

class Person { public $name; public function __toString() { return (string) $this->name; } } 

This then enables you to use the very super short:

array_walk($people, 'printf'); 

Syntax.

1 Comment

Looks like they're just StdClass objects in this case, but in general I think it's a good idea!
2

If you have array, you can use array_column for that:

$people = [ ['name' => 'John', 'age' => 3], ['name' => 'Carl', 'age' => 43] ]; var_dump( array_column($people, 'name') ); /* array(2) { [0]=> string(4) "John" [1]=> string(4) "Carl" } 

3 Comments

That's nice but only for arrays not objects
@MartinZeltin array_column works on arrays of objects, unless you're still using PHP 5.
this is exactly what i need
2

Since PHP 7.4 you can do:

echo implode('', array_map(fn($person) => $person->name . '<br>', $people)); 
  1. The inner array_map() function takes as second parameter your initial array $people.
  2. The inner array_map() function takes as first parameter an arrow function fn() => that gets the single items $person out of your initial array $people
  3. inside the arrow function each $person->name gets concated with '<br>' and written as value in a new array.
  4. this new array will be returned by the array_map() function
  5. The items of this array will be passed as second parameter to the implode() function and glued together with the empty string ''.
  6. the entire string that implode() returns gets echoed out

Comments

1

If you want to get "smart" about it you can write something like this:

 array_walk_recursive($a, function($v, $k) {echo $k === 'name' ? $v : '';}); 

But being smart over readability is a bad idea, always.

Comments

0

If I had to write a function to do that, it would be something along these lines:

function print_each($collection, $property) { foreach ($collection as $item) { echo $item->$property . "<br />"; } } 

Which could be used like so:

$people = [ (object)[ "name" => "Bob", "age" => 25 ], (object)[ "name" => "Dan", "age" => 31 ], (object)[ "name" => "Sally", "age" => 45 ] ]; print_each($people, "name"); // prints "Bob<br />Dan<br />Sally<br />" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.