1

I have an object in PHP and I'm using a

foreach ($items as $item)

to iterate through the items. However, $item contains another object called $item, which contains the $type variable whose value I need to access. How can I access the value of $type? What I want to do is:

foreach($items as item){
     if($item->item->type == 'this'){
             //do this
     } elseif ($item->item->type == 'that'){
             //do that
     }
}

But $item->item->type isn't picking up that value. How can I access it and use it for my function?

1
  • 2
    I dunno if it's relevant to anything other than your sample, but in your sample code 'item' in the foreach statement is missing the $ that should precede it. Commented Aug 24, 2011 at 4:52

1 Answer 1

2

have you tired:

foreach($items as item){
     if($item->type == 'this'){
             //do this
     } elseif ($item->type == 'that'){
             //do that
     }
}

or you can debug your code to find out what is going on:

foreach($items as item){
     print_r($item);
}

and then you can see what kind of childs this $item has.

Sign up to request clarification or add additional context in comments.

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.