TJ15
TJ15

Reputation: 363

Selecting item from multi-dimensional array

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?

Upvotes: 0

Views: 52

Answers (2)

Ares Draguna
Ares Draguna

Reputation: 1661

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

Upvotes: 1

Daan
Daan

Reputation: 12236

You need to access it like this:

$name = $tester[0]->name;
echo $name;

Upvotes: 2

Related Questions