Jump to content

How to fetch the value from stdclass object?


Recommended Posts

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 by mark107
Link to post
Share on other sites

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

 

Link to post
Share on other sites

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?

Link to post
Share on other sites

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.

Link to post
Share on other sites
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?

Link to post
Share on other sites

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']})";
}

 

Link to post
Share on other sites

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
}

 

Link to post
Share on other sites
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

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.