我有一个对象,其中包含要循环遍历的对象数组。对象的 print_r 如下所示:
SimpleXMLElement Object
(
[NewDataSet] => SimpleXMLElement Object
(
[Table1] => Array
(
[0] => SimpleXMLElement Object
(
[DiamondID] => 44696069
[ShapeTitle] => Pear
[Weight] => 0.300
[ColorTitle] => G
)
[1] => SimpleXMLElement Object
(
[DiamondID] => 44775332
[ShapeTitle] => Pear
[Weight] => 0.300
[ColorTitle] => G
)
[2] => SimpleXMLElement Object
(
[DiamondID] => 46959935
[ShapeTitle] => Pear
[Weight] => 0.340
[ColorTitle] => D
)
)
)
)
该对象来自我通过 SOAP 调用检索的一些 XML。
我想遍历“Table1”数组,访问每个对象。我这样做是通过:
foreach($rapnetresult->NewDataSet->Table1 as $itemno=>$diamond) {
echo "<p>Item #$itemno<br>";
echo "DiamondID: " . $diamond->DiamondID . "<br>";
echo "ShapeTitle: " . $diamond->ShapeTitle. "<br>";
echo "Weight: " . $diamond->Weight"</p>";
}
这会产生以下输出:
Item #Table1
DiamondID: 44696069
ShapeTitle: Pear
Weight: 0.300
Item #Table1
DiamondID: 44775332
ShapeTitle: Pear
Weight: 0.300
Item #Table1
DiamondID: 46959935
ShapeTitle: Pear
Weight: 0.340
这就是我想要的,我可以访问每个对象。但是我对为什么 $itemno 变量总是具有值“Table1”感到困惑。我本来希望它是 Table1 数组键,即:0、1、2 等。
谁能解释为什么我没有得到预期的密钥?我必须做什么才能得到钥匙?