0

I have an array with the following contents:

$tester

 array(1) {
  [0]=>
  object(CategoryItem)#79 (17) {
    ["type"]=>
    string(0) ""
    ["addedInVersion"]=>
    string(4) "0.02"
    ["lastUpdatedInVersion"]=>
    string(4) "0.02"
    ["AToZ"]=>
    bool(false)
    ["name"]=>
    string(22) "Page Name"
    ["scopeNotes"]=>
    string(0) ""
    ["historyNotes"]=>
    string(13) "Added in 0.02"
    ["broaderItems"]=>
    array(0) {
    }

I want to echo out the name, if this case Page Name and then use this in an if statement.

I have but this errors, I also tried $tester->CategoryItem->name but no joy.

Is there anything obvious I am missing?

2 Answers 2

2

You need to access it like this:

$name = $tester[0]->name;
echo $name;
Sign up to request clarification or add additional context in comments.

Comments

1

you have some leaks in your php OOP understanding, you should fix them by following some tutorials like these ones:

http://www.killerphp.com/tutorials/object-oriented-php/
http://code.tutsplus.com/tutorials/object-oriented-php-for-beginners--net-12762
http://www.tutorialspoint.com/php/php_object_oriented.htm

Now to answer your question, your code should be this:

$the_name = $tester[0]->name;
if($the_name == 'whatever value you want') {
   echo $the_name;
}

first of all, your initial variable is a array, therefor, $tester[0], then, this position is an object of the class CategoryItem so you use scope: $tester[0]->value

As for the last of your values in the class properties, broaderItems, this is again an array, so to access one of his values, you will have to call it like:

$tester[0]->broaderItems[0]; //or whatever the keys you will have here

Hope this helps! :D

1 Comment

;) glad to help! take care!

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.