I have an array like this (this is actually a WordPres开发者_C百科s 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 !
$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);
}
精彩评论