Hello

I am trying to get at a certain value in an array:

I have this $cats array:

which prints out this when I do a print_r($cats);
Array ( [0] => stdClass Object ( [term_id] => 1 [name] => Articles [slug] => articles [term_group] => 0 [term_taxonomy_id] => 1 [taxonomy] => category [description] => [parent] => 0 [count] => 6 [object_id] => 56 [cat_ID] => 1 [category_count] => 6 [category_description] => [cat_name] => Articles [category_nicename] => articles [category_parent] => 0 ) [1] => stdClass Object ( [term_id] => 5 [name] => Events [slug] => events [term_group] => 0 [term_taxonomy_id] => 5 [taxonomy] => category [description] => [parent] => 0 [count] => 6 [object_id] => 56 [cat_ID] => 5 [category_count] => 6 [category_description] => [cat_name] => Events [category_nicename] => events [category_parent] => 0 ) )

I want to get the 'name' items (for example Articles and the Events), so I am doing this in my code:

foreach($cats as $inner_arr) {
    	foreach($inner_arr as $key => $value) {
	     if ($key == 'name') {
        	echo $value . ', ';
	     }	
	}	
}

It does echo out the names correctly but I don't know if there is a better way to do this.

Also, I want to leave out the comma on the last loop...

any advice/ suggestions?

thanks

F

    Thanks for your response.

    So are you saying do this? :

    foreach($cats as $inner_arr) { 
      echo "$inner_arr[name], ";
    }
    

    I am getting an error so just double checking...

    (Fatal error: Cannot use object of type stdClass as array in...)

    thanks

      okay - will check it out. thanks again : )

        Write a Reply...