Today I learned about a powerful WordPress function that can create a new array from an object.
According to its WP Codex description, it does the ff:
Pluck a certain field out of each object in a list
(Kind of a straightforward definition lol)
// Say this is our array $player_info = array( array( 'name' => 'Kobe Bryant', 'team' => 'LAL', ), array( 'name' => 'Jeremy Lin', 'team' => 'HOU', ), array( 'name' => 'Damian Lillard', 'team' => 'POR', ), array( 'name' => 'LeBron James', 'team' => 'MIA', ), array( 'name' => 'Kevin Durant', 'team' => 'OKC', ), ); // Magic happens here.. $players = wp_list_pluck( $player_info, 'name' ); // Result is... array( 'Kobe Bryant', 'Jeremy Lin', 'Damian Lillard', 'LeBron James', 'Kevin Durant' );
So there.
I believe you can use this as a shortcut if you want to get just a single field out of, say, $posts. Instead of using the foreach, you can do a sweet one-liner.
Happy coding!
http://codex.wordpress.org/Function_Reference/wp_list_pluck