So it's really simple code and I am yanking my hair out with the problem.
//I first retrieve some JSON info (confirmed to work fine)
$file=file_get_contents('url');
//I then decode and print to verify (still working)
$somename=json_decode($file);
I print it out just to make sure it works (it does):
print_r($somename);
The print out reads as follows:
stdClass Object ( [id] => 456456456 [name] => somename [Stuff01] => 55 [Stuff02] => 25 [Stuff03] => 123132123132 ) )
Now I just want to get the value in the 'id' key so I use the appropriate object call:
$thisID=$somename->{'id'};
But I get the error:
Notice: Undefined property: stdClass::$id
I print_r every time so I know it's there. I can see it. What am I doing wrong?
I have had no problems doing this exact thing many times.
How do you access to individual properties is based on your data structure. You have kind of nested structure, object in object. Try like this:
$somename->somename->id;
//or
$yourObjectName->somename->id;
I hope this helps!
Related
I have class named Country->Gov->Branch. Each one of them returns an array to the other in this order : Country gives Gov and merges it with gov, gov gives branch and merges it with branch. Branch array is an attribute of User class when I use User and load it into $_session , I do merge the Branch array to session directly using array merge to make it easy to use so I dont have to loop array of array (Just an idea). The result of print_r($_SESSION); was as follows
Array ( [userid] => 1
[login] => email#gmail.com
[name] => Mohamed Talaat
[group] => 0
[picture] => mac
[Branchbranchid] => 1
[Branchbranchname] => Head Office
[Branchaddress] => xxxxxx st. , xxxx
[Branchgovid] => 2
[Govgovid] => 2
[Govgovcode] => 03
[Govgovname] => Alexandria
[Govcountryid] => 1
[Countrycountryid] => 1
[Countrycountrycode] => 20
[Countrycountryname] => Egypt )
I said Horray , but the happy feelings don`t last forever when I call it in the way below, I get an error.
echo $_SESSION['Branchbranchid'];
Notice: Undefined index: Branchbranchid in D:\xampp\htdocs\office\home.php on line 7
and nothing is echoed
I do have a session_start(); at the first head of the page before any code , i notice the problem happened at the merged part starting from the key [Branchbranchid] because if i do echo $_session['any element']; like
echo $_SESSION['name'];
Before this key ([Branchbranchid]), the code works fine. I tried to turn around by adding values with keys to another array but I get the same result and the same notice appear and nothing is echoed. I think there is a problem in the keys because when I have pushed the values to an indexed array, its working fine but without the keys, it is a lost case ...
Any ideas ... about why and How to avoid or to fix this issue
You should put start_session() as first line in your php file wherever you are trying to access the global session.
And change the $_session with $_SESSION
I'm trying to do an web app which people can see their status in League of Legends, but I don't even know how to do some things. I've got this class:
stdClass Object
(
[player] => stdClass Object
(
[id] => xxxxxx
[name] => yyyy
[profileIconId] => 627
[summonerLevel] => 30
[revisionDate] => 1422798145000
)
)
and im using this php code:
<?php
$summoner = 'yohanbdo';
$summonerdata = $leagueclass->getsummoner($summoner);
?>
I want to take only the id, name and profileIconId and show it. I don't know how to do this.
PD: My english isn't that good so thanks you all for the edits.
Weird, I was just looking that Riot API a little while ago.
I feel like you're pretty new to this type of notation, so I'll try and be quick but concise with my explanation.
What you have there, as Gerifield stated, are objects. You access their properties by using the -> operator. For example, if I assume that the object $main is what you're var_dumping out, then you could simply get the objects like so:
$main = json_decode($some_json_string);
//Now that we have the object set, we can deal with the properties.
echo $main->player->name;
//This will output the player name.
echo $main->player->id;
//Will output the player ID.
Notice in each case that since the player key of the $main object is also an object, it's properties must be accessed via the -> operator.
However, you could also simply use associative arrays by passing the second parameter to json_decode, like so:
$main = json_decode($some_json_string,TRUE);
echo $main['player']['id'];
echo $main['player']['name'];
Hopefully this helps.
I'm getting a Notice and a Warning here, not an error. My code still works. Just wanted to see if someone could figure out why I'm getting the notice and warning.
Notice: Trying to get property of non-object in file.php on line 152
Warning: in_array() expects parameter 2 to be array, null given file.php on line 152
Line 152 is an if() statement:
if($user->type == 'x' && in_array($user->email, $campaign->settings->email_list))
{ do stuff }
I've pinpointed the issue to the $campaign object. Using print_r on $campaign outputs quite a bit of info, but this is the important part:
Campaign Object
{
[settings] => stdClass Object
(
[email_list] => Array
(
[0] => support#domain.com
[1] => customer#domain.com
)
)
}
Obviously, $campaign->settings->email_list is an array. Why am I getting the Notice and Warning, then? $campaign is created directly above Line 152.
I think you may get this notice because of possible uninitialization (maybe null value) assigned to $campaign->settings campaign's internal object.
Please ensure with a var_dump($campaign->settings) what is the real value of the property before the line with in_array function.
try this some time it will works.
$campaign['settings']['email_list']
It might be the late hour or user error but I'm having trouble extracting variables from a PDO, where it would normally work. When I print_r my results by doing $array->results(), I get the following line:
Array ( [0] => stdClass Object ( [messageid] => 1 [fromid] => 2 [toid] => 1 [message] => Hello! ) )
However, on other projects when working with other people I have then just referred to the variable like this:
$result['message']
This is obviously after the line
$result = $array->results();
I've been troubleshooting and come to no conclusions, but I'm terrible at managing arrays so it could be something super simple. Any ideas? Thanks!
Since the result seems objects within array, so get your array and access object(s) inside it, to get message object from your $result, just do:
echo $result[0]->message;
I'm trying to create a custom user profile in Drupal 7. One of the fields references the user object, and I can't figure out what the syntax is to do that. I've read that '->' should be used, but I'm not sure exactly how.
The structure of a print_r($user_object) is at: http://pastebin.mozilla.org/1741565
I'm trying to get to the data inside field_country[data] - just not sure how to do it.
In addition, when I try to access $user_object[user_relationships_ui], nothing is visible. It doesn't throw an error, but doesn't print anything, either.
Thanks for the help
The way I figured out how to do it was to cast the object as an array -
$user_object= (array) $user_profile['field_country']['#object'];
After that, I could access it as a normal array.
Attributes of an object in php must be accessed by ->property_name. And, when using associated arrays, using ['key_name'].
Here, $user_object[0]['user_relationship_ui']['#type'] gives you the #type.
Don't confuse objects with associated arrays.
EDIT:
Further explaining,
and see line 18, 19:
[field_country] => Array (
[#theme] => field
see line 31, 32:
[#object] => stdClass Object (
[uid] => 1
The difference is if you want to access #theme, you have to do it by using ['#theme']. And if you want to access uid, you have to do ['#object']->uid.