Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
array (size=1)
  29 => 
    object(stdClass)[300]
      public 'term_id' => string '29' (length=2)
      public 'name' => string 'Advertisement' (length=13)
      public 'slug' => string 'advertisement' (length=13)
      public 'term_group' => string '0' (length=1)
      public 'term_taxonomy_id' => string '32' (length=2)
      public 'taxonomy' => string 'wp_portfolio_categories' (length=23)
      public 'description' => string '' (length=0)
      public 'parent' => string '27' (length=2)
      public 'count' => string '3' (length=1)
      public 'object_id' => string '536' (length=3)

I must get term_id value I can do this with $var[29]->term_id but I not know about this element 29, are any solution to get term_id ar 29 number

share|improve this question

closed as too localized by nickb, tereško, Eric, Eric J., ithcy Jan 30 at 1:12

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

4 Answers

up vote 1 down vote accepted

If the array allways contains only one element, use

$tmp=array_values($var);
$term_id=$tmp[0]->term_id;
share|improve this answer
Not olways, can be more than 1 element – Mantas Kudeikis Jan 29 at 20:55
In this case you need to be clear: Do you want all values (use foreach) or the first value (use my answer, it will work) – Eugen Rieck Jan 29 at 21:13

You can use foreach on the array (it cycles over all elements in the array giving you key and value):

 foreach ($var as $key=>$value) {
     // in this case $key will be 29, $value will be the object.
 }
share|improve this answer

Here is a one-liner for variety:

array_pop(array_values($var))->term_id;
share|improve this answer
1  
This changes the original array! Might not be a problem, but should be noted. – Eugen Rieck Jan 29 at 21:13

You can Use This code

foreach($array as $Key=>$val){

  if(isset($val->term_id)){
     //Your $Key
  }
}
share|improve this answer

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