我无法将对象类代码更改为array。当我在phpTester.net中执行php代码时,出现错误“ NOTICE Undefined index:tContinent on line number 9”。注意试图获取non属性-第9行上的对象。如何访问对象类的sCode元素?我新开始了php开发。
object(stdClass)#2 (1) { ["ListOfContinentsByNameResult"]=> object(stdClass)#3 (1) { ["tContinent"]=> array(6)
{ [0]=> object(stdClass)#4 (2) { ["sCode"]=> string(2) "AF" ["sName"]=> string(6) "Africa" }
[1]=> object(stdClass)#5 (2) { ["sCode"]=> string(2) "AN" ["sName"]=> string(10) "Antarctica" } [2]=> object(stdClass)#6 (2) { ["sCode"]=> string(2) "AS" ["sName"]=> string(4) "Asia" }
[3]=> object(stdClass)#7 (2) { ["sCode"]=> string(2) "EU" ["sName"]=> string(6) "Europe" }
[4]=> object(stdClass)#8 (2) { ["sCode"]=> string(2) "OC" ["sName"]=> string(8) "Ocenania" }
[5]=> object(stdClass)#9 (2) { ["sCode"]=> string(2) "AM" ["sName"]=> string(12) "The Americas" } } } }
<?php
$client = new SoapClient("http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL");
$con = $client->ListOfContinentsByName();
$Object= json_decode(json_encode($con), true);
$c=[];
for($i=0;$i<6;$i++)
{
$c[$i] = $Object["tContinent"][$i]->sCode;
}
for($i=0;$i<6;$i++)
{
echo $c[$i] . "<br/>\n";
}