0

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 !

1 Answer 1

4
$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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

You are a GENIUS ! I was spinning around for a good 3 hours and you solved it in 2 minutes :) THANKS !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.