我在回显会话数组时遇到了一个奇怪的问题。我想完成以下任务:
for($i=0;$i<sizeof($_SESSION['medication']);$i++){
echo $_SESSION['medication'][$i];
}
事实上,它会回显数组的所有字段,然后显示众所周知的错误消息“可捕获的致命错误:stdClass 类的对象无法转换为字符串”。
但是,当我在循环中一直回显字段 0 而不是字段 $i 时,它可以正常工作而不会出现错误消息。
为什么会触发错误消息?
更新 1
var_dump($_SESSION['medication'])
回响出一堆东西:
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" } }
更新 2
我发现了问题:稍后在代码中我使用变量 $medication 似乎是指会话。怎么会?register_globals 开启了吗?
UPDATE 3 - SOLUTION FOUND register_globals 确实打开了(多么尴尬),我把它关掉了。它与另一个变量 $medication 相关。现在它工作正常。谢谢大家!