使用3 foreach时php上的undefined属性

时间:2016-11-19 05:44:56

标签: php

当我试图回应它时,我尝试使用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(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;
        }
    }

如果我尝试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":""
                     }
                  ]
               }
            ]
         }
      ]
   }
}

1 个答案:

答案 0 :(得分:0)

<tr><td>$r->code</td><td>$rc->service</td><td>$rc->description</td><td>$rcc->etd</td><td>

无法转换,因为当我将其复制粘贴到我的记事本++时,“ - ”字符会消失,这就是为什么它表示类无法转换为字符串。