Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How do I loop through this array and get all the term_id's?

Array (
    [0] => stdClass Object (
        [term_id] => 43
    )
    [1] => stdClass Object (
        [term_id] => 25
    )
)

Cheers, Adnan

share|improve this question
    
foreach ($arr as $item) { var_dump($item); } –  zerkms Jan 12 at 20:41

2 Answers 2

up vote 1 down vote accepted
$ob1 = new stdClass();
$ob1->term_id = 43;

$ob2 = new stdClass();
$ob2->term_id = 25;

$scope = array($ob1,$ob2);

foreach($scope as $o){
  echo $o->term_id.'<br/>';
}

// Out
// 43
// 25
share|improve this answer
    
Thanks for the quick reply, that was what I wrote but I was outputting my code in in a select html element so I was looking in the wrong place haha, will accept in 4 min. –  adnan Jan 12 at 20:50
    
@adnan loop echo <option value="$o->term_id">....</option>; –  voodoo417 Jan 12 at 20:53

Each element of your array is an regular object, so yo can access it just by for (if elements of array are in order and keys are integers) or foreach (in examples $a is given array):

For:

$count = sizeof($a);
for ($i = $count; $i--;)
{
    echo $a[$i]->term_id;
}

Foreach:

foreach ($a as $item)
{
    echo $item->term_id;
}

If you want to add all ids to another array, you just need to write following code (in example for foreach):

$ids = array();
foreach ($a as $item)
{
    $ids[] = $item->term_id;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.