0

I am having a weird problem echoing out a session array. I would like to accomplish the following:

for($i=0;$i<sizeof($_SESSION['medication']);$i++){
    echo $_SESSION['medication'][$i];
}

In fact it echoes out all fields of the array and then afterwards displays the well known error message "Catchable fatal error: Object of class stdClass could not be converted to string".

However, when I just echo out field 0 all the time in the loop instead of field $i, it works fine without error message.

Why is there an error message triggered?

UPDATE 1

var_dump($_SESSION['medication']) echoes out a bunch of stuff:

array(12) { [0]=> string(1) "1" [1]=> int(10) [2]=> string(2) "14" [3]=> string(2) "17" [4]=> object(stdClass)#1 (7) { ["id"]=> string(1) "1" ["name"]=> string(9) "AUGMENTIN" ["strength"]=> string(6) "875 mg" ["sig"]=> string(29) "1 tablet by mouth twice a day" ["quantity"]=> string(6) "twenty" ["refills"]=> string(4) "zero" ["treatmentfor"]=> string(17) "sinus, bronchitis" } [5]=> object(stdClass)#2 (7) { ["id"]=> string(2) "10" ["name"]=> string(8) "DIFLUCAN" ["strength"]=> string(6) "150 mg" ["sig"]=> string(47) "1 tablet by mouth as needed for yeast infection" ["quantity"]=> string(3) "one" ["refills"]=> string(3) "one" ["treatmentfor"]=> string(27) "yeast, other abx for female" } [6]=> object(stdClass)#3 (7) { ["id"]=> string(2) "14" ["name"]=> string(14) "MEDROL DOSEPAK" ["strength"]=> string(1) "-" ["sig"]=> string(135) "6 PO Qday x 1 day, then 5 PO Qday x 1 day, then 4 PO Qday x 1 day, then 3 PO Qday x1 day, then 2 PO Qday x 1 day then 1 PO Qday x 1 day" ["quantity"]=> string(2) "21" ["refills"]=> string(4) "zero" ["treatmentfor"]=> string(17) "allergic rhinitis" } [7]=> object(stdClass)#4 (7) { ["id"]=> string(2) "17" ["name"]=> string(23) "FLUTICASONE NASAL SPRAY" ["strength"]=> string(6) "0.0005" ["sig"]=> string(91) "1 spray each nostril twice a day, reducing to 1 spray per nostril per day when appropriate." ["quantity"]=> string(10) "one bottle" ["refills"]=> string(3) "one" ["treatmentfor"]=> string(17) "allergic rhinitis" } [8]=> object(stdClass)#5 (7) { ["id"]=> string(1) "1" ["name"]=> string(9) "AUGMENTIN" ["strength"]=> string(6) "875 mg" ["sig"]=> string(29) "1 tablet by mouth twice a day" ["quantity"]=> string(6) "twenty" ["refills"]=> string(4) "zero" ["treatmentfor"]=> string(17) "sinus, bronchitis" } [9]=> object(stdClass)#6 (7) { ["id"]=> string(2) "10" ["name"]=> string(8) "DIFLUCAN" ["strength"]=> string(6) "150 mg" ["sig"]=> string(47) "1 tablet by mouth as needed for yeast infection" ["quantity"]=> string(3) "one" ["refills"]=> string(3) "one" ["treatmentfor"]=> string(27) "yeast, other abx for female" } [10]=> object(stdClass)#7 (7) { ["id"]=> string(2) "14" ["name"]=> string(14) "MEDROL DOSEPAK" ["strength"]=> string(1) "-" ["sig"]=> string(135) "6 PO Qday x 1 day, then 5 PO Qday x 1 day, then 4 PO Qday x 1 day, then 3 PO Qday x1 day, then 2 PO Qday x 1 day then 1 PO Qday x 1 day" ["quantity"]=> string(2) "21" ["refills"]=> string(4) "zero" ["treatmentfor"]=> string(17) "allergic rhinitis" } [11]=> object(stdClass)#8 (7) { ["id"]=> string(2) "17" ["name"]=> string(23) "FLUTICASONE NASAL SPRAY" ["strength"]=> string(6) "0.0005" ["sig"]=> string(91) "1 spray each nostril twice a day, reducing to 1 spray per nostril per day when appropriate." ["quantity"]=> string(10) "one bottle" ["refills"]=> string(3) "one" ["treatmentfor"]=> string(17) "allergic rhinitis" } } 

UPDATE 2

I have found the problem: later in the code I use the variable $medication which seems to refer to the session. How come? Is register_globals on?

UPDATE 3 - SOLUTION FOUND register_globals was indeed on (how embarrassing) and I turned it off. It related to the other variable $medication. Now it works fine. Thanks to everyone!

4
  • show us the var_dump($_SESSION['medication']) please. Thanks. Commented May 29, 2012 at 22:40
  • try a var_dump($_SESSION['medication']); Commented May 29, 2012 at 22:40
  • what are you storing in 'medication'? Is it an array? Show more hint. Commented May 29, 2012 at 22:40
  • @tunmisefasipe I am storing different medications in the session array. Please see question edit for var_dump() Commented May 29, 2012 at 22:45

3 Answers 3

1

My guess is that you have something other than a string stored in that index of the session array. In this case, it seems you have a stdClass object in the array.

Also, since you can have non-numeric array indices, using a foreach loop will yield better results.

foreach($_SESSION['medication'] as $index => $value) {
    if (is_scalar($value)) {
        echo "$index = $value<br />";
    } else {
        echo "<pre>$index = " . print_r($value, true) . "</pre><br />";
    }
}

is_scalar will check to see if the variable can be echo'ed (string, int, float, bool), otherwise we will print_r the value.

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

1 Comment

This was very helpful and showed me what was in the array so thank you very much!!
1

Why are you trying to loop over an array with a for? foreach was designed specifically to iterate over arrays.

Try:

foreach($_SESSION['medication'] as $foo){
    echo $foo;
}

You're likely storing an object in $_SESSION['medication'] as others have suggested. You should try var_dump($_SESSION['medication']); to see what's actually stored in there, if there is actually an object (stdClass) in there, you'll want to remove it before running your loop as you can't echo a class.

Edit:

$_SESSION['medication'][4] and all elements after that contain an object as the value, you'll have to do something other than echo it (try print_r like others have suggested)

1 Comment

I normally use foreach, too, but it has the same problem.
0

Theres an object of type StdClass in that session array that cant be echo'd

Also, use a foreach

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.