开发者

Exctract values from php array

开发者 https://www.devze.com 2023-04-03 11:45 出处:网络
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 ) )

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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消