Issue accessing objects within objects in a file with XML format - php

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;
}

Related

Loop through JSON arrays using PHP [duplicate]

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.

Access the values in array php

I am trying to access the values of an array.
Array ( [0] => stdClass Object ( [action_type] => comment [value] => 19074.125 ) [1] => stdClass Object ( [action_type] => like [value] => 2179.9 ) [2] => stdClass Object ( [action_type] => link_click [value] => 16.633202528886 ) [3] => stdClass Object ( [action_type] => offsite_conversion.fb_pixel_add_to_cart [value] => 451.45857988166 ) [4] => stdClass Object ( [action_type] => offsite_conversion.fb_pixel_complete_registration [value] => 897.60588235294 ) [5] => stdClass Object ( [action_type] => offsite_conversion.fb_pixel_initiate_checkout [value] => 1230.5887096774 ) [6] => stdClass Object ( [action_type] => offsite_conversion.fb_pixel_lead [value] => 372.17804878049 ) [7] => stdClass Object ( [action_type] => offsite_conversion.fb_pixel_purchase [value] => 2384.265625 ) [8] => stdClass Object ( [action_type] => offsite_conversion.fb_pixel_view_content [value] => 63.316597510373 ) [9] => stdClass Object ( [action_type] => post [value] => 8477.3888888889 ) [10] => stdClass Object ( [action_type] => post_like [value] => 193.15569620253 ) [11] => stdClass Object ( [action_type] => unlike [value] => 76296.5 ) [12] => stdClass Object ( [action_type] => page_engagement [value] => 15.168290258449 ) [13] => stdClass Object ( [action_type] => post_engagement [value] => 15.274574574575 ) [14] => stdClass Object ( [action_type] => offsite_conversion [value] => 43.39960182025 ) )
I need to access the value of [action_type] => offsite_conversion.fb_pixel_purchase [value] => 2384.265625 , how to get the value of only offsite_conversion.fb_pixel_purchase ??
these values comes from the foreach loop.
$r = $i->cost_per_action_type;
foreach($r as $key => $value){
echo $key["action_type"];
}
the array you are seeing is the print_r($x).
If you want to use the result as an array and not as object, use:
//will return an associative array
$x = json_decode($j, true);
In your code $x is an object, not array, so that foreach is incorrect. You should work on your code and give some reasons for witch you encoded an array just to decode it and try to use it like an array not as object.
You should change the Object to array if you are using the array and got rid of the encode.
You can do what #Chay22 did in the example as array:
foreach($r as $val){
if ($val[action_type] == 'offsite_conversion.fb_pixel_purchase') {
echo $val[value];
}
}
You're trying to treat object as array which doesn't simply working. Use arrow operator -> instead to get object value.
foreach($r as $val){
echo $val->action_type;
}
In case you're really wanted it to be an array, make sure to implement interfaces that gives ability to treat object as an array ArrayAccess, Countable, etc. You can also extends ArrayObject class, too.
And, according to your comment
foreach($r as $val){
if ($val->action_type == 'offsite_conversion.fb_pixel_purchase') {
echo $val->value;
}
}

Getting objects instead of object properties in array

When I set $sales like so $sales = $response->xml->transactions the contents look like:
SimpleXMLElement Object
(
[transaction] => Array
(
[0] => SimpleXMLElement Object
(
[transId] => 9999999999
[submitTimeUTC] => 2016-03-16T21:57:54Z
[submitTimeLocal] => 2016-03-16T14:57:54
[transactionStatus] => capturedPendingSettlement
[invoiceNumber] => 4b1008a87f5262f0c867
[firstName] => Foo
[lastName] => Bar
[accountType] => Visa
[accountNumber] => XXXX1414
[settleAmount] => 155.00
[marketType] => eCommerce
[product] => Card Not Present
)
[1] => SimpleXMLElement Object
(
[transId] =>
So ->transaction contains an array. But if I do this:
$sales = $response->xml->transactions->transaction;
foreach ($sales as $s) {
$list[]= $s->invoiceNumber;
}
$list contains
Array
(
[0] => SimpleXMLElement Object
(
[0] => ac502c094fe1722ba100
)
[1] => SimpleXMLElement Object
(
[0] => e2eb58351c87155e3720
)
[2] => SimpleXMLElement Object
(
[0] => 0bca2bb6d5a13e641b67
)
What am I doing wrong? Where are the SimpleXMLElement Objects coming from where I just expected the invoiceNumber string?
The reason you are getting the SimpleXMLElement type objects is because when you are doing $list[]= $s->invoiceNumber; you are adding the element as it is. Since it is part of the SimpleXMLElement object it has that type internally. This comes up a lot when working with SimpleXML so its good to keep in mind.
As mentioned in the comments casting to a string will give you just the value:
$list[] = (string) $s->invoiceNumber;

Returning an array within multiple objects and arrays

I can't seem to figure out the correct syntax to print:
[Agriculture] => AGR
[Animals] => AN
[Arts and Humanities] => ART
within the $TOPIC object.
What makes this a bit more confusing is that $TOPIC is in an object that can be called in a different order than this example. So instead of [3] => stdClass Object, it could be [4] => stdClass Object, (or any number).
Some sample foreach syntax is included below; it doesn't work though.
stdClass Object
(
[123456] => stdClass Object
(
[required_actions] => Array
(
[1] => stdClass Object
(
[maxlength] =>
[value] => $MESSAGE
[options_hash] =>
)
[2] => stdClass Object
(
[maxlength] =>
[value] => $NAME_PREFIX
[options_hash] => stdClass Object
(
[Ms.] => Ms.
[Mrs.] => Mrs.
[Mr.] => Mr.
)
)
[3] => stdClass Object
(
[maxlength] =>
[value] => $TOPIC
[options_hash] => stdClass Object
(
[Agriculture] => AGR
[Animals] => AN
[Arts and Humanities] => ART
)
)
)
)
)
foreach ($json->123456->required_actions as $info) {
echo $info->value => $TOPIC->options_hash;
}
The solution is to search for the parent object by value, and then request the child object's array. Here is what I used:
foreach ($json->123456->required_actions as $info) {
if ('$TOPIC' == $info->value) {
print_r($info->options_hash);
}
}

How can I merge or search an object?

Here's my issue:
I have an object filled with arrays that look like this.
[376339] => Array
(
[0] => 1f422730-f54b-4e4d-9289-10258ce74446
[1] => 60dc4646-06ce-44d0-abe9-ee371847f4df
)
I need to search another object to find objects with the matching IDs, like below. Is there a way of doing this without a foreach? There are SEVERAL and I would like to not have to loop over the entire object every time.
stdClass Object
(
[id] => 1f422730-f54b-4e4d-9289-10258ce74446
[percentage] => 32
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 59826
[destination_id] => 59826
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxx
)
)
)
stdClass Object
(
[id] => 60dc4646-06ce-44d0-abe9-ee371847f4df
[percentage] => 68
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 60046
[destination_id] => 60046
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxxx
)
)
)
I need it to end up looking like this.
[376339] => Array
(
[0] => Array
(
[id] => 1f422730-f54b-4e4d-9289-10258ce74446
[percentage] => 32
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 59826
[destination_id] => 59826
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxx
)
)
)
[1] => Array
(
[id] => 60dc4646-06ce-44d0-abe9-ee371847f4df
[percentage] => 68
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 60046
[destination_id] => 60046
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxxx
)
)
)
)
I'm not sure if this makes any sense, that's why I had my two inital outputs I need to have merged into one somehow. This is all coming from one huge json object and I'm just using json_decode($jsonStuff) to decode it.
Would this be easier if I added true in the decode function? If I could just search for it like I could in python, that would be neat. But as it is, I'm at a loss as to how to get the output I need.
Note: Input json CANNOT be changed, I have no affiliation with the people that created it.
First loop over your input array and create an array with the key as the id
$input = json_decode($json_input);
$output = array();
foreach($input as $obj){
$output[$obj->id] = $obj;
}
then you can build your other array by searching the id on the array key
$massive_search_array = array(376339 => array
(
0 => 1f422730-f54b-4e4d-9289-10258ce74446,
1 => 60dc4646-06ce-44d0-abe9-ee371847f4df
)
);
$final_output = array();
foreach($massive_search_array as $index => $searches){
foreach($searches as $search){
if(isset($output[$search])){
$final_output[$index][] = $output[$search];
}
}
}

Categories