0

How to get value form array that have sbtClass

So, i use curl in php to get data trough API. This below is my code

$ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data02);

        $resp = curl_exec($ch);

        if($e = curl_error($ch)) {
            echo $e;
        } else {
            $decode = json_decode($resp);
            if(property_exists($stdClass, "code")) {
                echo $stdClass->object;
            }
        }
        curl_close($ch);

And the API result

object(stdClass)#2 (3) {
  ["result"]=>
  bool(true)
  ["data"]=>
  array(90) {
    [0]=>
    object(stdClass)#3 (6) {
      ["code"]=>
      string(10) "VAL125-S45"
      ["game"]=>
      string(8) "Valorant"
      ["name"]=>
      string(10) "125 Points"
      ["price"]=>
      object(stdClass)#4 (3) {
        ["basic"]=>
        int(13390)
        ["premium"]=>
        int(13325)
        ["special"]=>
        int(13260)
      }
      ["server"]=>
      string(2) "45"
      ["status"]=>
      string(5) "empty"
    }
  }
  ["message"]=>
  string(35) "Success to get data."
}

The question is, how to get each value like for example "code" or "game" or "name"?

Less 24H
  • 1
  • 2
  • If you struggle with iterating through an object you can alternatively add `true` as the 2nd argument to `json_decode` to force the results as an array.. ie: `$decode = json_decode($resp,true);` then you have an array to deal with instead. The properties that you are wanting do not exist at the level within the json data that you are targeting - possibly something like ``foreach( $decode->data as $index => $arr ) echo $data['code'],$data['name'],$data['game'];`` – Professor Abronsius Sep 03 '22 at 16:26

0 Answers0