Rob
Rob

Reputation: 305

PHP: Get value out of array

Simple probably but can't get it right. I need a value out of an session array here's how its build:

print_r($_SESSION); //gives:

Array
(
    [cart] => cart Object
        (
            [config] => Array()
            [maincurrency:cart:private] => GBP
         )
)

The code so far:

foreach($_SESSION['cart'] as $category => $thing) { 

    echo $category; //echo's config |the value I need GBP
    echo $thing; // echo's Array

if ($category == 'maincurrency:cart:private')
    {
        echo 'found_it'; //doesn't echo
        echo $category; //echo's nothing |the value I need GBP
        echo $thing; // echo's nothing

    }
}

The string I need is 'GBP' from maincurrency:Test:private.

Upvotes: 0

Views: 253

Answers (1)

dev-null-dweller
dev-null-dweller

Reputation: 29462

$_SESSION['test'] is not an array - it is an object of Test class with property maincurrency which access is limited to private - it means you can not directly access this property.

To get it's value you have either:

  • change access of property to public in class definition
  • create getter function for this property and use it to get it's value

Upvotes: 3

Related Questions