undefined property on php while using 3 foreach

i tried to make array processing easier with 3 foreach, when i tried to echo it,

it says

Catchable fatal error: Object of class stdClass could not be converted to string in

on this line

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>";

so how can i make it works ?

here my code :

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 (class)

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;
        }
    }

here's the output if i tried to 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/64866.html

上一篇: 如何在php中访问数字索引数组的对象

下一篇: 在使用3个foreach时未定义的属性