I have an array like this (this is actually a WordPress category list array):
Array ( [0] => stdClass Object ( [term_id] => 4 ) [1] => stdClass Object ( [term_id] => 6 ) )
Is there a way to exctract "term_id" values and assign it to $term variable with comma separated values?
in this case variable should be: $term = 4,6
Thanks for the help !
asked Sep 6, 2011 at 10:47
Peter
1,3065 gold badges20 silver badges41 bronze badges
1 Answer 1
$term = implode(',', array_map(function($o) { return $o->term_id; }, $array));
Or:
$term = array();
foreach($array as $o) $term[] = $o->term_id;
$term = implode(',', $term);
In a function:
function getTerms($array) {
$term = array();
foreach($array as $o) $term[] = $o->term_id;
return implode(',', $term);
}
answered Sep 6, 2011 at 10:48
Arnaud Le Blanc
100k24 gold badges211 silver badges196 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Peter
You are a GENIUS ! I was spinning around for a good 3 hours and you solved it in 2 minutes :) THANKS !
lang-php