user3070003
user3070003

Reputation:

php: get a variable inside an array

I have this array called $locations and this is what it produces if I use print_r:

Array
(
    [0] => stdClass Object
    (
        [term_id] => 40
        [name] => California
        [slug] => california
        [term_group] => 0
        [term_taxonomy_id] => 41
        [taxonomy] => location
        [description] =>
        [parent] => 0
        [count] => 6
     )
) 

What I need to get from this array is California only, but I can't figure out what variable produces that. I tried $locations->name and $locations[name] but none of those work.

Upvotes: 0

Views: 60

Answers (2)

Tommos
Tommos

Reputation: 870

$locations seems to be an array (with 1 value, that is an object), so:

$locations[0]->name

Upvotes: 3

hsz
hsz

Reputation: 152216

Just try with:

$data = array( /* your data */ );

foreach ($data as $obj) {
  $name = $obj->name; // California - and others in the nest steps 
                      // if array contains more elements
}

Upvotes: 0

Related Questions