在使用3个foreach时未定义的属性
我试着用3个foreach让数组处理更容易,当我试图回显它时,
它说
可捕获致命错误:类stdClass的对象无法转换为字符串
在这条线上
echo "<tr><td>$r>code</td><td>$rc>service</td><td>$rc>description</td><td>$rcc>etd</td><td>".number_format($rcc->value)."</td></tr>";
所以我怎么才能使它工作?
在这里我的代码:
process.php
case 'cost':
$origin = $_GET['origin'];
$destination = $_GET['destination'];
$weight = $_GET['weight'];
$courier = $_GET['courier'];
$cost = $IdmoreRO->hitungOngkir($origin,$destination,$weight,$courier);
//parse json
$costarray = json_decode($cost);
$results = $costarray->rajaongkir->results;
if(!empty($results)):
foreach($results as $r):
foreach($r->costs as $rc):
foreach($rc->cost as $rcc):
echo "<tr><td>$r>code</td><td>$rc>service</td><td>$rc>description</td><td>$rcc>etd</td><td>".number_format($rcc>value)."</td></tr>";
endforeach;
endforeach;
endforeach;
endif;
//end of parse json
break;
idmore.php(班级)
public function hitungOngkir($origin,$destination,$weight,$courier)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://rajaongkir.com/api/starter/cost",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>"origin=$origin&destination=$destination&weight=$weight&courier=$courier",
CURLOPT_HTTPHEADER => array("key: 3f01f13ce2b42ba983ad3f3bc4852f84"),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
$result = 'error';
return 'error';
} else {
return $response;
}
}
这里是输出,如果我试图print_r
{
"rajaongkir":{
"query":{
"origin":"363",
"destination":"210",
"weight":200,
"courier":"pos"
},
"status":{
"code":200,
"description":"OK"
},
"origin_details":{
"city_id":"363",
"province_id":"11",
"province":"Jawa Timur",
"type":"Kabupaten",
"city_name":"Ponorogo",
"postal_code":"63411"
},
"destination_details":{
"city_id":"210",
"province_id":"5",
"province":"DI Yogyakarta",
"type":"Kabupaten",
"city_name":"Kulon Progo",
"postal_code":"55611"
},
"results":[
{
"code":"pos",
"name":"POS Indonesia (POS)",
"costs":[
{
"service":"Surat Kilat Khusus",
"description":"Surat Kilat Khusus",
"cost":[
{
"value":20000,
"etd":"2",
"note":""
}
]
},
{
"service":"Express Next Day",
"description":"Express Next Day",
"cost":[
{
"value":34500,
"etd":"1",
"note":""
}
]
}
]
}
]
}
}
<tr><td>$r->code</td><td>$rc->service</td><td>$rc->description</td><td>$rcc->etd</td><td>
不能被转换,因为“ - ”字符消失时,我复制粘贴到我的记事本++,这就是为什么说类不能转换为字符串。
链接地址: http://www.djcxy.com/p/64865.html上一篇: undefined property on php while using 3 foreach
下一篇: php Converting an object implementing ArrayAccess in array