I have a PHP array of objects and would like to get all the post_id values easily. I have looked into things like array_values and am not really sure what to do with this. Is the best way to just use a for loop and add each post_id to another array with all the post ids?
Thanks for the help!
[0] => stdClass Object
(
[post_id] => 70242
[image_id] => 70244
[large_image] => 0
)
[1] => stdClass Object
(
[post_id] => 70327
[image_id] => 70339
[large_image] => 1
)
[2] => stdClass Object
(
[post_id] => 70017
[image_id] => 70212
[large_image] => 1
)
EDIT:
I am getting this array from a WordPress db call:
$q = <<<SQL
SELECT post_id, image_id, large_image
FROM $homepage_db
ORDER BY position;
SQL;
$results = $wpdb->get_results($q);
And then $results
is the array above
Just use a
foreach
loopusing
foreach
using
array_map
I prefer
foreach
in this caseEasy. Use
array_map
. Code below; used JSON for your data to test & demonstrate the concept while retaining your example structure:And the dumped output would be: