huseyinergen Posted October 16, 2023 Share Posted October 16, 2023 Hello everyone. i have this problem like this. Can anybody help me? Result of var_dump command below and i want to get value "switch_1". object(stdClass)#12 (4) { ["result"]=> array(6) { [0]=> object(stdClass)#3 (2) { ["code"]=> string(9) "work_mode" ["value"]=> string(6) "colour" } [1]=> object(stdClass)#5 (2) { ["code"]=> string(12) "bright_value" ["value"]=> int(255) } [2]=> object(stdClass)#8 (2) { ["code"]=> string(10) "temp_value" ["value"]=> int(255) } [3]=> object(stdClass)#9 (2) { ["code"]=> string(11) "colour_data" ["value"]=> string(28) "{"h":0.0,"s":255.0,"v":25.0}" } [4]=> object(stdClass)#10 (2) { ["code"]=> string(10) "switch_led" ["value"]=> bool(false) } [5]=> object(stdClass)#11 (2) { ["code"]=> string(8) "switch_1" ["value"]=> bool(true) } } ["success"]=> bool(true) ["t"]=> int(1697436125386) ["tid"]=> string(32) "884a243f6be911eeab9046acc027565a" } Quote Link to comment https://forums.phpfreaks.com/topic/317372-how-to-fetch-the-value-from-stdclass-object/ Share on other sites More sharing options...
requinix Posted October 16, 2023 Share Posted October 16, 2023 object(stdClass)#12 (4) { The value you dumped is an object. ["result"]=> array(6) { It has a "result" property, which is an array. [...]=> object(stdClass)#11 (2) { Somewhere in that array (you should probably not rely on it being in a particular place) is an object. ["code"]=> string(8) "switch_1" It has a "code" property, which is a string with the value "switch_1". ["value"]=> bool(true) That same object has a "value" property, which is the boolean value true. This is an awkward structure to work with. What if you wanted other things besides the switch_1? Wouldn't it be nice if you had a single object/array that had everything at once? Try this: // assuming $object was the variable you dumped, $results = array_reduce($object->results, function($array, $result) { $array[$result->code] = $result->value; return $array; }, []); Then try dumping out $results and see if the data is easier to use in that form. Quote Link to comment https://forums.phpfreaks.com/topic/317372-how-to-fetch-the-value-from-stdclass-object/#findComment-1612387 Share on other sites More sharing options...
huseyinergen Posted October 16, 2023 Author Share Posted October 16, 2023 8 hours ago, requinix said: object(stdClass)#12 (4) { The value you dumped is an object. ["result"]=> array(6) { It has a "result" property, which is an array. [...]=> object(stdClass)#11 (2) { Somewhere in that array (you should probably not rely on it being in a particular place) is an object. ["code"]=> string(8) "switch_1" It has a "code" property, which is a string with the value "switch_1". ["value"]=> bool(true) That same object has a "value" property, which is the boolean value true. This is an awkward structure to work with. What if you wanted other things besides the switch_1? Wouldn't it be nice if you had a single object/array that had everything at once? Try this: // assuming $object was the variable you dumped, $results = array_reduce($object->results, function($array, $result) { $array[$result->code] = $result->value; return $array; }, []); Then try dumping out $results and see if the data is easier to use in that form. Resuit is; i got an error. Thank you for your response Warning: array_reduce() expects parameter 1 to be array, null given in /volume1/web/tuya/sadece/get.php on line 76 Quote Link to comment https://forums.phpfreaks.com/topic/317372-how-to-fetch-the-value-from-stdclass-object/#findComment-1612398 Share on other sites More sharing options...
ginerjm Posted October 16, 2023 Share Posted October 16, 2023 So you are showing us your newest error message. So what do you think you need to do now? Perhaps echo the prameters you are using in the call to "array_reduce" ? That would tell you something. Like the first parameter that is currently null. Which means that your logic is flawed and you are not providing the needed data to this instruction. Quote Link to comment https://forums.phpfreaks.com/topic/317372-how-to-fetch-the-value-from-stdclass-object/#findComment-1612400 Share on other sites More sharing options...
huseyinergen Posted October 16, 2023 Author Share Posted October 16, 2023 All Codes are below. Please help me. I can not find any solution.. <?php $config = [ "secretKey" => "xxxxxxxx" , "accessKey" => "xxxxxxxx" , 'baseUrl' => 'https://openapi.tuyaeu.com' , ]; require( 'TuyaApi.php' ); $device_id = 'xxxxxx'; $tuya = new \tuyapiphp\TuyaApi( $config ); $token = $tuya->token->get_new( )->result->access_token; $tuya->devices( $token )->get_app_list( $app_id ); // Get device status $sonuc = $tuya->devices( $token )->get_status($device_id); echo "<pre>"; //$object = (object) $sonuc; var_dump($sonuc); echo "</pre>"; echo "<br>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/317372-how-to-fetch-the-value-from-stdclass-object/#findComment-1612403 Share on other sites More sharing options...
ginerjm Posted October 16, 2023 Share Posted October 16, 2023 By 'All Codes' are you saying that you are showing us all of your php programming? I doubt that is true but don't understand what else you are saying. I see what looks like 2 possible errors in this code. How about you add the following to the beginning of this code and running it? Then send us the error messages and point out the lines that are causing them. error_reporting(E_ALL); ini_set('display_errors', '1'); Quote Link to comment https://forums.phpfreaks.com/topic/317372-how-to-fetch-the-value-from-stdclass-object/#findComment-1612404 Share on other sites More sharing options...
huseyinergen Posted October 16, 2023 Author Share Posted October 16, 2023 Hello. Thank you for response. This Codes controlling or getting status of tuya devices(wifi smart switch). When i sending "get_status" command, i got stdclass results (please look first message) get status command ($sonuc line). I want to get switch_1 status from $sonuc results... Quote Link to comment https://forums.phpfreaks.com/topic/317372-how-to-fetch-the-value-from-stdclass-object/#findComment-1612405 Share on other sites More sharing options...
ginerjm Posted October 16, 2023 Share Posted October 16, 2023 It appears from what you have posted that 'switch_1' is nothing more than a value. Don't now what you think could provide a 'status' of that value. Bye. Quote Link to comment https://forums.phpfreaks.com/topic/317372-how-to-fetch-the-value-from-stdclass-object/#findComment-1612407 Share on other sites More sharing options...
huseyinergen Posted October 16, 2023 Author Share Posted October 16, 2023 I don't understand you... Quote Link to comment https://forums.phpfreaks.com/topic/317372-how-to-fetch-the-value-from-stdclass-object/#findComment-1612409 Share on other sites More sharing options...
ginerjm Posted October 16, 2023 Share Posted October 16, 2023 (edited) You said you want to get switch_1 status. To me (if I understand you) that means you expect that the string 'switch_1' has some kind of relationship to something called 'status' or something that you consider to be a 'status'. Well - if I understand this code (not 'codes') of yours there is none. Now - If you said you were looking for an element of your object that had a code of 'switch_1' and wanted to find the 'value' of that element then I could make sense of your dilemma. Edited October 16, 2023 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/317372-how-to-fetch-the-value-from-stdclass-object/#findComment-1612410 Share on other sites More sharing options...
requinix Posted October 16, 2023 Share Posted October 16, 2023 Read my description of the var_dump output, then look closely at the code I posted (hint: the error message tells you exactly where to look). There was a typo. 1 Quote Link to comment https://forums.phpfreaks.com/topic/317372-how-to-fetch-the-value-from-stdclass-object/#findComment-1612411 Share on other sites More sharing options...
huseyinergen Posted October 17, 2023 Author Share Posted October 17, 2023 (edited) 8 hours ago, requinix said: Read my description of the var_dump output, then look closely at the code I posted (hint: the error message tells you exactly where to look). There was a typo. i was try. Codes here; Quote <?php error_reporting(E_ALL); ini_set('display_errors', '1'); $config = [ "secretKey" => "xxxx" , "accessKey" => "xxxx" , 'baseUrl' => 'https://openapi.tuyaeu.com' , ]; require( 'TuyaApi.php' ); $device_id = 'xxxx'; $tuya = new \tuyapiphp\TuyaApi( $config ); $token = $tuya->token->get_new( )->result->access_token; //$tuya->devices( $token )->get_app_list( $app_id ); // Get device status $sonuc = $tuya->devices( $token )->get_status($device_id); echo "<pre>"; //$object = (object) $sonuc; var_dump($sonuc); echo "</pre>"; echo "<br>"; // assuming $object was the variable you dumped, $results = array_reduce($sonuc->results, function($array, $result) { $array[$result->code] = $result->value; return $array; }, []); var_dump($results); ?> Results; Quote object(stdClass)#12 (4) { ["result"]=> array(6) { [0]=> object(stdClass)#4 (2) { ["code"]=> string(9) "work_mode" ["value"]=> string(6) "colour" } [1]=> object(stdClass)#5 (2) { ["code"]=> string(12) "bright_value" ["value"]=> int(255) } [2]=> object(stdClass)#8 (2) { ["code"]=> string(10) "temp_value" ["value"]=> int(255) } [3]=> object(stdClass)#9 (2) { ["code"]=> string(11) "colour_data" ["value"]=> string(28) "{"h":0.0,"s":255.0,"v":25.0}" } [4]=> object(stdClass)#10 (2) { ["code"]=> string(10) "switch_led" ["value"]=> bool(false) } [5]=> object(stdClass)#11 (2) { ["code"]=> string(8) "switch_1" ["value"]=> bool(true) } } ["success"]=> bool(true) ["t"]=> int(1697521609927) ["tid"]=> string(32) "910eb9e06cb011eeab9046acc027565a" } Notice: Undefined property: stdClass::$results in /volume1/web/tuya/sadece/get.php on line 32 Warning: array_reduce() expects parameter 1 to be array, null given in /volume1/web/tuya/sadece/get.php on line 35 NULL Where is the problem in my codes Edited October 17, 2023 by huseyinergen Wrong words Quote Link to comment https://forums.phpfreaks.com/topic/317372-how-to-fetch-the-value-from-stdclass-object/#findComment-1612417 Share on other sites More sharing options...
requinix Posted October 17, 2023 Share Posted October 17, 2023 Read my description of the var_dump output, then look closely at the code I posted (hint: the error message tells you exactly where to look). There was a typo. 1 Quote Link to comment https://forums.phpfreaks.com/topic/317372-how-to-fetch-the-value-from-stdclass-object/#findComment-1612418 Share on other sites More sharing options...
huseyinergen Posted October 17, 2023 Author Share Posted October 17, 2023 I didn't find a typo in your post I was trying all possibilities. Quote Link to comment https://forums.phpfreaks.com/topic/317372-how-to-fetch-the-value-from-stdclass-object/#findComment-1612419 Share on other sites More sharing options...
huseyinergen Posted October 17, 2023 Author Share Posted October 17, 2023 5 hours ago, requinix said: Read my description of the var_dump output, then look closely at the code I posted (hint: the error message tells you exactly where to look). There was a typo. i need help Quote Link to comment https://forums.phpfreaks.com/topic/317372-how-to-fetch-the-value-from-stdclass-object/#findComment-1612433 Share on other sites More sharing options...
requinix Posted October 18, 2023 Share Posted October 18, 2023 Remember the part where I said On 10/16/2023 at 2:12 AM, requinix said: ["result"]=> array(6) { It has a "result" property, which is an array. And remember the part where I posted On 10/16/2023 at 2:12 AM, requinix said: $results = array_reduce($object->results, function($array, $result) { Take the time to understand what I was describing about how to read the var_dump output, then look at that one line of code I posted to see if it makes sense according to what I described. It should not make sense, and you should be able to spot a small error. I'm trying to get you to learn here, instead of just accepting what a person on the internet says and copy/pasting their code. You can't be a chef by copying someone else, and you can't be a programmer by copying someone else. Quote Link to comment https://forums.phpfreaks.com/topic/317372-how-to-fetch-the-value-from-stdclass-object/#findComment-1612448 Share on other sites More sharing options...
aarti789 Posted October 25, 2023 Share Posted October 25, 2023 Can you try the below code, I hope you can get what you are looking for. Quote <?php // Your JSON data as a string (you can also decode JSON from a file or API) $jsonData = '{ "result": [ { "code": "work_mode", "value": "colour" }, { "code": "bright_value", "value": 255 }, { "code": "temp_value", "value": 255 }, { "code": "colour_data", "value": "{"h":0.0,"s":255.0,"v":25.0}" }, { "code": "switch_led", "value": false }, { "code": "switch_1", "value": true } ], "success": true, "t": 1697436125386, "tid": "884a243f6be911eeab9046acc027565a" }'; // Decode the JSON data into a PHP object $data = json_decode($jsonData); // Initialize the value variable to null $switchValue = null; // Loop through the "result" array to find the "switch_1" code foreach ($data->result as $result) { if ($result->code === "switch_1") { $switchValue = $result->value; break; } } // Check if "switch_1" was found and display the value if ($switchValue !== null) { echo 'Value for "switch_1" is: ' . $switchValue; } else { echo 'Value for "switch_1" not found.'; } ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/317372-how-to-fetch-the-value-from-stdclass-object/#findComment-1612558 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.