0

我在回显会话数组时遇到了一个奇怪的问题。我想完成以下任务:

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 相关。现在它工作正常。谢谢大家!

4

3 回答 3

1

为什么要尝试使用 a 遍历数组forforeach专门设计用于迭代数组。

尝试:

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

您可能会$_SESSION['medication']按照其他人的建议存储一个对象。您应该尝试var_dump($_SESSION['medication']);查看其中实际存储的内容,如果其中实际上有一个对象 ( stdClass),则您需要在运行循环之前将其删除,因为您无法回显类。

编辑:

$_SESSION['medication'][4]并且之后的所有元素都包含一个对象作为值,除了回显它之外,您还必须做一些事情(尝试 print_r 就像其他人建议的那样)

于 2012-05-29T22:41:19.363 回答
1

我的猜测是,除了存储在会话数组的索引中的字符串之外,还有其他内容。在这种情况下,您似乎stdClass在数组中有一个对象。

此外,由于您可以使用非数字数组索引,因此使用 foreach 循环将产生更好的结果。

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将检查变量是否可以回显(字符串、整数、浮点数、布尔值),否则我们将打印_r

于 2012-05-29T22:41:33.753 回答
0

该会话数组中有一个 StdClass 类型的对象无法回显

另外,使用foreach

于 2012-05-29T22:41:13.647 回答