0

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.

6
  • does $category echo 'maincurrency:Test:private'? Commented Oct 24, 2011 at 23:00
  • 1
    You have a semi-colon at the end of your if-statement that you should remove (I'm sure this is only in the post, not the actual code). Commented Oct 24, 2011 at 23:00
  • Does $_SESSION['test']['maincurrency:Test:private'] equal GBP? Commented Oct 24, 2011 at 23:02
  • something's not right with your condition if $category is outputting config right there...or your print_r of $_SESSION['test'] doesn't match what you're saying. Commented Oct 24, 2011 at 23:06
  • You are sure that $_SESSION['test'] is the containing array? Commented Oct 24, 2011 at 23:07

1 Answer 1

3

$_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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Didn't knew that. I'm gone write some extra code then.

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.