mark107 Posted December 2, 2019 Share Posted December 2, 2019 (edited) Hi all, I need some help with my code as I have a hard time with fetching the value from the stdclass. I am using json data to convert it to PHP so I would like to fetch the "appears" data from the stdclass object. When I try this: echo $data->appears; I'm getting this: Notice: Trying to get property of non-object in /home/username/public_html/foldername/script.php on line 22 I have also tried this: echo $data[0]->appears->value; And this: echo $data->appears[0]->value; I am still get the same error so I dont know what to do to resolve it. Full code: <?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); $json = '{"data":{"185.220.101.46":{"domains_count":0,"domains_list":null,"updated":"2019-12-02 22:04:25","spam_rate":1,"frequency":6827,"frequency_time_24h":106,"frequency_time_1h":2,"network_type":"hosting","in_antispam":1,"in_security":0,"appears":1,"country":"DE","submitted":"2018-01-11 20:34:37","frequency_time_10m":2,"sha256":"e39d4a9be2f210d1a75ba2b5ece08a4b35e99002b03aeb6ceaad1d98de87c248"}}}'; // Converts it into a PHP object $data = json_decode($json, true); //print_r($data); echo $data["appears"]->value ."<br/>"; //var_dump($data[1]->{'updated'}); //echo $data->updated[1]->value; ?> STDClass object: Array ( [data] => Array ( [185.220.101.46] => Array ( [domains_count] => 0 [domains_list] => [updated] => 2019-12-02 22:04:25 [spam_rate] => 1 [frequency] => 6827 [frequency_time_24h] => 106 [frequency_time_1h] => 2 [network_type] => hosting [in_antispam] => 1 [in_security] => 0 [appears] => 1 [country] => DE [submitted] => 2018-01-11 20:34:37 [frequency_time_10m] => 2 [sha256] => e39d4a9be2f210d1a75ba2b5ece08a4b35e99002b03aeb6ceaad1d98de87c248 ) ) ) Var_dump: array(1) { ["data"]=> array(1) { ["185.220.101.46"]=> array(15) { ["domains_count"]=> int(0) ["domains_list"]=> NULL ["updated"]=> string(19) "2019-12-02 22:04:25" ["spam_rate"]=> int(1) ["frequency"]=> int(6827) ["frequency_time_24h"]=> int(106) ["frequency_time_1h"]=> int(2) ["network_type"]=> string(7) "hosting" ["in_antispam"]=> int(1) ["in_security"]=> int(0) ["appears"]=> int(1) ["country"]=> string(2) "DE" ["submitted"]=> string(19) "2018-01-11 20:34:37" ["frequency_time_10m"]=> int(2) ["sha256"]=> string(64) "e39d4a9be2f210d1a75ba2b5ece08a4b35e99002b03aeb6ceaad1d98de87c248" } } } Can you please show me an example how I can fetch the `appears` from the stdclass object? Any advice would be much appreciated. Edited December 2, 2019 by mark107 Quote Link to comment https://forums.phpfreaks.com/topic/309615-how-to-fetch-the-value-from-stdclass-object/ Share on other sites More sharing options...
Barand Posted December 2, 2019 Share Posted December 2, 2019 $data = json_decode($json, true); That converts it into an array. To convert it to an object, remove the "true". $data = json_decode($json); Quote Link to comment https://forums.phpfreaks.com/topic/309615-how-to-fetch-the-value-from-stdclass-object/#findComment-1572097 Share on other sites More sharing options...
requinix Posted December 2, 2019 Share Posted December 2, 2019 You're way off. For one, it's not an object. In fact you explicitly told PHP that you did not want an object. So it gave you an array. If you want an object then do what Barand said. For two, you don't seem to have any idea how the data is structured. Maybe something will click if you look at indented print_r output? Array ( [data] => Array ( [185.220.101.46] => Array ( [domains_count] => 0 [domains_list] => [updated] => 2019-12-02 22:04:25 [spam_rate] => 1 [frequency] => 6827 [frequency_time_24h] => 106 [frequency_time_1h] => 2 [network_type] => hosting [in_antispam] => 1 [in_security] => 0 [appears] => 1 [country] => DE [submitted] => 2018-01-11 20:34:37 [frequency_time_10m] => 2 [sha256] => e39d4a9be2f210d1a75ba2b5ece08a4b35e99002b03aeb6ceaad1d98de87c248 ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/309615-how-to-fetch-the-value-from-stdclass-object/#findComment-1572098 Share on other sites More sharing options...
mark107 Posted December 2, 2019 Author Share Posted December 2, 2019 T 32 minutes ago, Barand said: $data = json_decode($json, true); That converts it into an array. To convert it to an object, remove the "true". $data = json_decode($json); Thanks for your advice, I have removed it but I cant get the value of `appears` from the array as I am getting the same error. Any idea? Quote Link to comment https://forums.phpfreaks.com/topic/309615-how-to-fetch-the-value-from-stdclass-object/#findComment-1572100 Share on other sites More sharing options...
Barand Posted December 2, 2019 Share Posted December 2, 2019 Here's a clue Quote Link to comment https://forums.phpfreaks.com/topic/309615-how-to-fetch-the-value-from-stdclass-object/#findComment-1572101 Share on other sites More sharing options...
mark107 Posted December 2, 2019 Author Share Posted December 2, 2019 16 minutes ago, Barand said: Here's a clue I have tried this: echo $data->data['185.220.101.46']->appears; it doesn't work. Any idea? It would be easier if you could post the code of what i should use. Quote Link to comment https://forums.phpfreaks.com/topic/309615-how-to-fetch-the-value-from-stdclass-object/#findComment-1572102 Share on other sites More sharing options...
requinix Posted December 2, 2019 Share Posted December 2, 2019 You are mixing object and array access together. It will never work. It will always be only object access or only array access. Because "185.220.101.46" is not (normally) a valid property name for an object, I suggest sticking with the array data from json_decode. Then use only array notation when trying to get the value. Quote Link to comment https://forums.phpfreaks.com/topic/309615-how-to-fetch-the-value-from-stdclass-object/#findComment-1572103 Share on other sites More sharing options...
mark107 Posted December 2, 2019 Author Share Posted December 2, 2019 1 minute ago, requinix said: You are mixing object and array access together. It will never work. It will always be only object access or only array access. Because "185.220.101.46" is not (normally) a valid property name for an object, I suggest sticking with the array data from json_decode. Then use only array notation when trying to get the value. I cant remove the "185.220.101.46" because it come with it when i'm getting access to the site which i am not allowed to post it. So what I have to do then? Create a foreach loop to fetch the values and store in the array? Quote Link to comment https://forums.phpfreaks.com/topic/309615-how-to-fetch-the-value-from-stdclass-object/#findComment-1572104 Share on other sites More sharing options...
requinix Posted December 2, 2019 Share Posted December 2, 2019 I'm currently at work dealing with some stupid off-shore developers so I'm just going to cut to the chase here: $data = json_decode($json, true); echo $data['data']['185.220.101.46']['appears']; echo $data['data'][key($data['data'])]['appears']; foreach ($data['data'] as $ipaddress => $result) { echo "({$ipaddress}: {$result['appears']})"; } Quote Link to comment https://forums.phpfreaks.com/topic/309615-how-to-fetch-the-value-from-stdclass-object/#findComment-1572105 Share on other sites More sharing options...
Barand Posted December 2, 2019 Share Posted December 2, 2019 Even though the IP stops you accessing the value directly (if it's an object) as in echo $data->data->185.220.101.46->appears; … you can access it by using iteration $data = json_decode($json); // decode as object foreach ($data->data as $ip=>$ipdata) { echo "$ip : {$ipdata->appears}"; //--> 185.220.101.46 : 1 } Quote Link to comment https://forums.phpfreaks.com/topic/309615-how-to-fetch-the-value-from-stdclass-object/#findComment-1572106 Share on other sites More sharing options...
requinix Posted December 2, 2019 Share Posted December 2, 2019 6 minutes ago, Barand said: you can access it by using iteration Oh right, PHP allows iterating over members of an object... shudder There is a syntactical way though, which should just about never be used $data->data->{"185.220.101.46"}->appears Quote Link to comment https://forums.phpfreaks.com/topic/309615-how-to-fetch-the-value-from-stdclass-object/#findComment-1572108 Share on other sites More sharing options...
Barand Posted December 2, 2019 Share Posted December 2, 2019 Aha! I tried echo $data->data->"185.220.101.46"->appears; and I tried echo $data->data->{185.220.101.46}->appears; but I didn't think of trying both together. Quote Link to comment https://forums.phpfreaks.com/topic/309615-how-to-fetch-the-value-from-stdclass-object/#findComment-1572109 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.