This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I'm trying to loop through an array of moves for a Pokemon website. I'm using the API PokeApi (https://pokeapi.co/).
My question is how can I access the moves in these arrays using plain PHP.
I've tried using this just to call 1 move. But I don't know how to access data that's in arrays. Like "move->version-group-details".
$base = "https://pokeapi.co/api/v2/pokemon/";
$id = 1;
$data = file_get_contents($base.$id."/");
$pokemon = json_decode($data);
echo $pokemon->moves[0];
Thanks in advance :)
So you have two methods here you can do so when I run
$base = "https://pokeapi.co/api/v2/pokemon/";
$id = 1;
$data = file_get_contents($base.$id."/");
$pokemon = json_decode($data);
print_r($pokemon->moves[0]);
I get the result:
stdClass Object
(
[move] => stdClass Object
(
[name] => razor-wind
[url] => https://pokeapi.co/api/v2/move/13/
)
[version_group_details] => Array
(
[0] => stdClass Object
(
[level_learned_at] => 0
[move_learn_method] => stdClass Object
(
[name] => egg
[url] => https://pokeapi.co/api/v2/move-learn-method/2/
)
[version_group] => stdClass Object
(
[name] => crystal
[url] => https://pokeapi.co/api/v2/version-group/4/
)
)
[1] => stdClass Object
(
[level_learned_at] => 0
[move_learn_method] => stdClass Object
(
[name] => egg
[url] => https://pokeapi.co/api/v2/move-learn-method/2/
)
[version_group] => stdClass Object
(
[name] => gold-silver
[url] => https://pokeapi.co/api/v2/version-group/3/
)
)
)
)
If you want to access a moves name you will have to run $pokemon->moves[0]->move->name since we are getting an object returned. If you want to get the name inside the move_learn_method of version_group_details you will have to run
$pokemon->moves[0]->version_group_details[0]-> move_learn_method->name
Alternatively, if you want to return all arrays instead of objects just run this
$base = "https://pokeapi.co/api/v2/pokemon/";
$id = 1;
$data = file_get_contents($base.$id."/");
$pokemon = json_decode($data, true);
print_r($pokemon['moves'][0]);
This will now return
Array
(
[move] => Array
(
[name] => razor-wind
[url] => https://pokeapi.co/api/v2/move/13/
)
[version_group_details] => Array
(
[0] => Array
(
[level_learned_at] => 0
[move_learn_method] => Array
(
[name] => egg
[url] => https://pokeapi.co/api/v2/move-learn-method/2/
)
[version_group] => Array
(
[name] => crystal
[url] => https://pokeapi.co/api/v2/version-group/4/
)
)
[1] => Array
(
[level_learned_at] => 0
[move_learn_method] => Array
(
[name] => egg
[url] => https://pokeapi.co/api/v2/move-learn-method/2/
)
[version_group] => Array
(
[name] => gold-silver
[url] => https://pokeapi.co/api/v2/version-group/3/
)
)
)
)
So instead of having to use the object accessor -> you can access the data by using array notation so instead of
$pokemon->moves[0]->version_group_details[0]-> move_learn_method->name
you can now use:
$pokemon['moves']['version_group_details'][0]['move_learn_method']['name']
Hope that helped.
Currently I have a stdclass object. How can I access the properties of the object such as the name of the author? I have tried to echo out $authors = $book->authors_summary[0]->name; but it returns 'Trying to get property of non-object in ...' error.
[authors_summary] => Array
(
[0] => Array
(
[id] => 123456789
[name] => Ben Smith
)
[1] => Array
(
[nconst] => 987654321
[name] => Tommy Lee
)
)
Your authors_summary[0] is an array, so you cannot use -> object pointer, you must call it as array:
$authors = $book->authors_summary[0]['name'];
'name' is the key of array so you may access name value like this
$authors = $book->authors_summary[0]['name'];
echo $authors;
Array
(
[data] => Array
(
[0] => stdClass Object
(
[Id] => 5
[Name] => nilu
[Email] => nilu#gmail.com
[Password] => nilu
[Image] => LOVE_A_O_D.jpg
)
)
)
how to fetch password from this.....
in controller----
$datas['data']= $this->Login_model->loginchk($value);
in model-----
$results=$this->db->get();
return $results->result();
I have a XML format file which has objects inside objects. This is how I get values of first object, which works fine for me:
$soapclient = new SoapClient('http://anywb/book.asmx?WSDL');
$params = array('ISBN' => "1111");
$response = $soapclient->GetBookByISBN($params);
//This will give me the value "Success"
$result = $response->GetBookByISBNResult->ResponseText;
Now my question is how to access the object which is inside object. For example, how to get "BookID" which is 4 and how to get value of "Type" which is 1?
Any suggestion would be appreciated. Here is the object:
stdClass Object
(
[GetBookByISBN] => stdClass Object
(
[ResponseText] => Success
[SearchResult] => stdClass Object
(
[Search] => Array
(
[0] => stdClass Object
(
[Date] => 2015-10-20
[BookID] => 4
[Discription] => stdClass Object
(
[Type] => 1
)
[Probability] => stdClass Object
(
[Kids] =>
[Adult] => 00
)
)
[1] => stdClass Object
(
[Date] => 2016-11-15
[BookID] => 5
[Discription] => stdClass Object
(
[Type] => 2
)
[Probability] => stdClass Object
(
[Kids] =>
[Adult] => 00
)
)
))))
You do it like this:
$response->GetBookByISBN->SearchResult->Search[0]->BookID;
$response->GetBookByISBN->SearchResult->Search[0]->Discription->Type;
If you would like to have an array containing all book ids you would have something like this:
$BookIDs = array();
foreach($response->GetBookByISBN->SearchResult->Search as $key => $value) {
$BookIDs[$key] = $value->BookID;
}
Or if you would like to be able to modify the values in $response using the $BookIDs (as an example) iot would look like this:
$BookIDs = array();
foreach($response->GetBookByISBN->SearchResult->Search as $key => $value) {
$BookIDs[$key] = &$value->BookID;
}
I'm trying to loop through a stdClass object ($args) which is formatted as shown below. The goal is to loop through each of the sObjects, JSON encode the values and send it to an endpoint.
I figured this would do the trick:
$object = $args->Notification->sObject;
foreach($object as $item) { JSON_ENCODE AND SEND TO PASTEB.IN }
Except I just get a 'null' response.
If I do this:
$object = $args->Notification;
foreach($object as $item) {
$sf = $item->sObject;"
// JSON_ENCODE $sf and send to PASETBI.N
}
... I get the result of only one result posted to pasteb.in, not all of them (e.g 3 separate results as should be the case in the below table).
I've had a look at answers to other questions regarding looping through arrays/sObjects but I can't get it right. Any help would be much appreciated!
Print_r'ing this object shows this format:
stdClass Object
(
[OrganizationId] => XXXXXXXXXXXXXXXX
[ActionId] => XXXXXXXXXXXXXXXX
[SessionId] =>
[EnterpriseUrl] => https://darlston-dev-ed.my.salesforce.com/services/Soap/c/33.0/XXXXXXXXXXXXXXXX
[PartnerUrl] => https://darlston-dev-ed.my.salesforce.com/services/Soap/u/33.0/XXXXXXXXXXXXXXXX
[Notification] => Array
(
[0] => stdClass Object
(
[Id] => XXXXXXXXXXXXXXXX
[sObject] => stdClass Object
(
[Id] => XXXXXXXXXXXXXXXX
[Contact_Email__c] => test#testemail.com
)
)
[1] => stdClass Object
(
[Id] => XXXXXXXXXXXXXXXX
[sObject] => stdClass Object
(
[Id] => XXXXXXXXXXXXXXXX
[Contact_Email__c] => test#testemail.com
)
)
[2] => stdClass Object
(
[Id] => XXXXXXXXXXXXXXXX
[sObject] => stdClass Object
(
[Id] => XXXXXXXXXXXXXXXX
[Contact_Email__c] => test#testemail.com
)
)
)
)
Can you try this?
foreach($args->Notification as $item)
{
echo $item->Id."<br>";
echo $item->sObject->Id."<br>";
echo $item->sObject->Contact_Email__c."<br>";
}
I have the following text below, what is the easiest way to parse through it?
if(typeof sa_inst.apiCB == 'function') sa_inst.apiCB({"AS":{"Query":"g","FullResults":1,"Results":[{"Type":"AS","Suggests":[{"Txt":"g<strong>oogle</strong>","Type":"AS","Sk":""},{"Txt":"g<strong>mail</strong>","Type":"AS","Sk":"AS1"},{"Txt":"g<strong>oogle</strong> <strong>maps</strong>","Type":"AS","Sk":"AS2"},{"Txt":"g<strong>oogle</strong> <strong>earth</strong>","Type":"AS","Sk":"AS3"},{"Txt":"g<strong>ames</strong>","Type":"AS","Sk":"AS4"},{"Txt":"g<strong>oogle</strong> <strong>images</strong>","Type":"AS","Sk":"AS5"},{"Txt":"g<strong>amestop</strong>","Type":"AS","Sk":"AS6"},{"Txt":"g<strong>rainger</strong>","Type":"AS","Sk":"AS7"}]}]}} /* pageview_candidate */);
I would like to only get what is after ("Txt":) and not including the <strong> or anything else. For example, form the text above I would like to get something like:
google, gmail, google maps, google images, etc
in an array. Help please?
The text is JSON. Use json_decode to parse it.
$str = '{"AS":{"Query":"g","FullResults":1,"Results":[{"Type":"AS","Suggests":[{"Txt":"g<strong>oogle</strong>","Type":"AS","Sk":""},{"Txt":"g<strong>mail</strong>","Type":"AS","Sk":"AS1"},{"Txt":"g<strong>oogle</strong> <strong>maps</strong>","Type":"AS","Sk":"AS2"},{"Txt":"g<strong>oogle</strong> <strong>earth</strong>","Type":"AS","Sk":"AS3"},{"Txt":"g<strong>ames</strong>","Type":"AS","Sk":"AS4"},{"Txt":"g<strong>oogle</strong> <strong>images</strong>","Type":"AS","Sk":"AS5"},{"Txt":"g<strong>amestop</strong>","Type":"AS","Sk":"AS6"},{"Txt":"g<strong>rainger</strong>","Type":"AS","Sk":"AS7"}]}]}}';
$result = json_decode($str);
//Show all 'Txt' values
foreach($result->AS->Results[0]->Suggests as $suggests)
echo "Txt contains: ".htmlspecialchars($suggests->Txt). "<br/>";
//Show full contents of result
echo "<pre>";
print_r($result);
This outputs values of Txt first:
Txt contains: g<strong>mail</strong>
Txt contains: g<strong>oogle</strong> <strong>maps</strong>
Txt contains: g<strong>oogle</strong> <strong>earth</strong>
Txt contains: g<strong>ames</strong>
Txt contains: g<strong>oogle</strong> <strong>images</strong>
Txt contains: g<strong>amestop</strong>
Txt contains: g<strong>rainger</strong>
And then the complete JSON data too:
stdClass Object
(
[AS] => stdClass Object
(
[Query] => g
[FullResults] => 1
[Results] => Array
(
[0] => stdClass Object
(
[Type] => AS
[Suggests] => Array
(
[0] => stdClass Object
(
[Txt] => google
[Type] => AS
[Sk] =>
)
[1] => stdClass Object
(
[Txt] => gmail
[Type] => AS
[Sk] => AS1
)
[2] => stdClass Object
(
[Txt] => google maps
[Type] => AS
[Sk] => AS2
)
[3] => stdClass Object
(
[Txt] => google earth
[Type] => AS
[Sk] => AS3
)
[4] => stdClass Object
(
[Txt] => games
[Type] => AS
[Sk] => AS4
)
[5] => stdClass Object
(
[Txt] => google images
[Type] => AS
[Sk] => AS5
)
[6] => stdClass Object
(
[Txt] => gamestop
[Type] => AS
[Sk] => AS6
)
[7] => stdClass Object
(
[Txt] => grainger
[Type] => AS
[Sk] => AS7
)
)
)
)
)
)
This will work, but I removed ' from 'function'
<?php
echo $text = 'if(typeof sa_inst.apiCB == function sa_inst.apiCB({"AS":{"Query":"g","FullResults":1,"Results":[{"Type":"AS","Suggests":[{"Txt":"g<strong>oogle</strong>","Type":"AS","Sk":""},{"Txt":"g<strong>mail</strong>","Type":"AS","Sk":"AS1"},{"Txt":"g<strong>oogle</strong> <strong>maps</strong>","Type":"AS","Sk":"AS2"},{"Txt":"g<strong>oogle</strong> <strong>earth</strong>","Type":"AS","Sk":"AS3"},{"Txt":"g<strong>ames</strong>","Type":"AS","Sk":"AS4"},{"Txt":"g<strong>oogle</strong> <strong>images</strong>","Type":"AS","Sk":"AS5"},{"Txt":"g<strong>amestop</strong>","Type":"AS","Sk":"AS6"},{"Txt":"g<strong>rainger</strong>","Type":"AS","Sk":"AS7"}]}]}} /* pageview_candidate */);';
echo '<br />';
$text = strip_tags($text); // remove html tags
$a = explode('"Txt":"', $text);
$app = array();
foreach ($a as $c) {
$b = explode('"', $c);
$app = $b[0];
}
unset($app[0]);
var_dump($app);
?>