1

I have something like this. I want to access "name" attribute. I can access it by

foreach($items as $item)
{echo $item->name}

I have tried echo $items[0]['name'] but it doesn't work.

    Array 
   ( 
      [0] => stdClass Object 
        ( 
               [term_id] => 7 
               [name] => Video/Animacija 
               [slug] => videoanimacija 
               [term_group] => 0 
               [term_taxonomy_id] => 7 
               [taxonomy] => portfolio_technologies 
               [description] => 
               [parent] => 0 
               [count] => 17 [filter] => raw 
         ) 
    )

4 Answers 4

6

You need to use the object syntax on name there as well.

echo $items[0]->name;

should work. Remember, you have the following structures

  • an array (access via [])
  • an object (access via ->)
Sign up to request clarification or add additional context in comments.

1 Comment

I've never realized that [] and -> Thank you :D
3

$item[0] is a std object, and you cannot access to its value using brackets: [], you should use arrow: ->

Like this :

$item[0]->name

Comments

2

You have an array of objects, not an array of arrays. You need to do this:

$variable = $items[0]->name;

Comments

1

You don't have an array inside an array, you have an object inside an array. You need to access it like:

echo $items[0]->name;

Comments

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.