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.
Related
I am facing a strange problem. It may just be a silly mistake, and I am just missing some basics.
I'm running php 5.6.1 on MAMP.
I have a simple array which I get from a mysql query. Using a foreach loop, I can print_r() each value, which gives me: stdClass Object ( [srno] => 6 [link] => this-is-link )
Now I can echo $obj->srno, and that prints fine. But I can't use echo $obj['srno'] which I was previously using, on an older version of PHP, but- It shows nothing.
Any help really appreciated. Thanks!
If you have a stdClass object and need to address it as an array, you can cast it to array quite easily:
$someObj = new stdClass();
$someObj->foo = "bar";
$someArray = (array)$someObj; // Cast the object to an array
echo $someArray['foo']; // Will give you "bar"
Working example: http://3v4l.org/nni1Y
Of course as comments already pointed out, you may want to look at retrieving your mysql results as an array in the first place.
As you said your results return as an object so you can use it by using
$obj->your_field_name to display field value. But your can not use by $obj['field_name'];
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 have a PHP script that dumps data from an API.
The dump is an array
print_r($answer);
outputs
Array ( [success] => 1 [serial] => s001 [url] => http://gooole.com )
I want to have another variable called $url that holds the value url from the array (held in $answer) in PHP.
I'm unfamiliar with this.
check out extract() it will take the keys from an array, and create variable of the same name to store them in. There are a few flags you can pass it, to determine exactly what it does with things like pre-existing variables of the same name.
EDIT: as mentioned in the comments on your question, though, $url = $answer['url']; is probably the simplest way to go.
I am working response from an API which returns data in JSON in a fairly straight forward structure. Pseudo structure is best represented as:
services -> service -> options -> option -> suboptions -> option
As this is decoded JSON, all of these are stdClass objects. I have a number of foreach statements iterating through the various services and options, which all work without issue. However, when I use a foreach statement at the suboption level, the object is serialized into a string. For your information suboptions has a structure like this.
[suboptions] => stdClass Object
(
[option] => stdClass Object
(
[code] => SOME_CODE
[name] => Some Name
)
)
When using a foreach such as this (where $option is an option in services -> service -> options -> option):
foreach($option->suboptions->option as $suboption) {
print_r($suboption);
}
It outputs
SOME_CODESome Name
Not the expected
stdClass Object
(
[code] => SOME_CODE
[name] => Some Name
)
I am not aware of any reasons foreach would do this in terms of depth or other conditions.
I've tried everything I can think of and searched through SO and can't find any case of this happening. If anyone has any ideas I'd love to hear them! Cheers.
Apologies if it has been answered elsewhere or I am missing something obvious. If I have, it has escaped me thus far.
Edit: For those asking it is indeed the AusPost PAC API. Changing over from the DRC as it is being removed ~2014.
I can't comment, because I don't have that amount of reputation, but I'll try to answer...
I'm assuming you want to remove the stdClass from the PHP array?
If so, then make sure you are using true when decoding the JSON.
Example: $array = json_decode($json, true);
For more information on stdClass, see this SO post: What is stdClass in PHP?
Again, forgive me if I'm misinterpreting your question, I just wish I could comment...
I'm assuming this is from a response from the Australia Post postage calculation API. This API makes the horrible decision to treat collection instances with only one item as a single value. For example, service -> options -> option may be an array or it may be a single option.
The easiest way I've found to deal with this is cast the option to an array, eg
$options = $service->options->option;
if (!is_array($options)) {
$options = array($options);
}
// now you can loop over it safely
foreach ($options as $option) { ... }
You would do the same thing with the sub-options. If you're interested, I've got an entire library that deals with the AusPost API, using Guzzle to manage the HTTP side.
The values of the last object are not arrays so you would need both the key and value.
foreach($option->suboptions->option as $key => $value) {
echo '<p>KEY:'.$key.' VALUE:'.$value.'</p>';
}
I'm learning PHP and Drupal. I need to reference a variable contained in an array called $contexts.
So print_r($contexts) gives me this:
Array (
[context_view_1] => ctools_context Object (
[type] => view
[view] => view Object (
[db_table] => views_view
[result] => Array (
[0] => stdClass Object (
[nid] => 28
[node_data_field_display_field_display_value] => slideshow
)
)
eek confusing. I want to work with the node_data_field_display_field_display_value variable. I think my code needs to be like this, but I know this isn't right:
if ($contexts['context_view_1']['view']['result'][0]
['node_data_field_display_field_display_value'] == 'slideshow') then do whatever...
Thanks!
You suggested the following array reference to get to the variable you want:
$contexts['context_view_1']['view']['result'][0]['node_data_field_display_field_display_value']
The reason this doesn't work is because some of the structures in the chain are actually objects rather than arrays, so you need a different syntax for them to get at their properties.
So the first layer is correct, because $contexts is an array, so context_view_1 is an array element, so you'd get to it with $contexts['context_view_1'] as you did.
But the next level is an object, so to get to view, you need to reference it as an object property with -> syntax, like so: $contexts['context_view_1']->view
For each level down the tree, you need to determine whether it's an object or an array element, and use the correct syntax.
In this case, you'll end up with something that looks like this:
$context['context_view_1']->view->result[0]->node_data_field_display_field_display_value
That's a mess of a variable. The issue you're having is that you're using the bracketed notation, e.g. "['view']", for each "step" in the navigation through your variable. That would be fine if each child of the variable were an array, but not every one is.
You'll note, for example, that $contexts['context_view_1'] is actually an object, not an array (take note that it says "[context_view_1] => ctools_context Object"). Whereas you would use that bracketed notation to address the elements of an array, you use the arrow operator to address the properties of an object.
Thus, you would address the field you are trying to reach with the following expression:
$contexts['context_view_1']->view->result[0]->node_data_field_display_field_display_value
For properties listed as "Object", you need to use -> to get into it, and "Array", you need to use []. So:
$contexts['context_view_1']->view->result[0]->node_data_field_display_field_display_value
$contexts['context_view_1']->view->result[0]->node_data_field_display_field_display_value
echo $context['context_view_1']->view->result[0]->node_data_field_display_field_display_value;
Do not mistake objects with arrays. A memeber of an array can be accesed by $array['member'], but fields of an object can be accessed as $object->fieldname.