How to reconstruct object from information given by PHP print_r() - php

I after using PHP SoapClient to call a service method, I get return value $result, which when print_r($result) gives:
stdClass Object
(
[GetDataRowResult] => stdClass Object
(
[FieldValueList] => stdClass Object
(
[FieldValuePair] => Array
(
[0] => stdClass Object
(
[Field] => Name
[Value] => Christmas Party
)
[1] => stdClass Object
(
[Field] => Status
[Value] => 3
)
[2] => stdClass Object
(
[Field] => StartDate
[Value] => 18/12/2009 12:00 AM
)
[3] => stdClass Object
(
[Field] => EndDate
[Value] => 01/01/1900 12:00 AM
)
)
)
[Message] =>
[Success] => 1
)
)
I want to use the status value to do something, but I don't know how to get to that value. I tried $result->GetDataRowResult->FieldValueList->FieldValuePair[1]->Value and it didn't work, which I sort of expected. EDIT: It did work actually, I had another typo error in the code else where to cause it to go wrong
How do I get to the value I need and is there a better way to recontruct the entire output into a PHP object?

use this
$result->GetDataRowResult->FieldValueList->FieldValuePair->1->Value

Related

how get value feild[ID] stdClass Object in php code

how get value feild[id] in php code
stdClass Object ( [List_inserted] => Array ( [0] => stdClass Object ( [ID] => 145001 [value] => 40 ) ) [Sucssess] => 1 [ErrorMassage] => OK )
You didn't give us a name of stdClass so I'm assuming it's $stdClass.
$stdClass->List_inserted[0]->ID
Let's break it down;
stdClass Object ( [List_inserted] => Array ( [0] => stdClass Object ( [ID] => 145001 [value] => 40 ) ) [Sucssess] => 1 [ErrorMassage] => OK )
We access objects with a -> and we access arrays with []
The first part tells us it's an object, so it's;
$stdClass->List_inserted
List_inserted is an array thanks to the => Array. We can access this with [0].
$stdClass->List_inserted[0]
Well, List_inserted[0] is an object, thanks too [0] => stdClass Object; and you wanted to access the ID? So we need another ->
$stdClass->List_inserted[0]->ID

Checking if XML Attributes exist in loop

When looping through the below xml object, I need to get the data out of each attribute, and if attributes dont exist, move on. I am not sure how to check to see if #attributes even exists. Currently, if it doesn't my loop dies.
I have tried:
if(isset($v[0]->user->attributes())){
....
but that gives me a php error:
Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
So I am not sure how to check if it exist, if it does i can run through the loop. if it doesnt i can move on...
[server] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 17980
)
[user] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 1075159
[default-loc] => 50000
[status] => 1
[license] => high,standard
[password] => aebecf880f2060c31e44f820785e0aa63ea4cc44
[primary] => 50000
[username] => 17980_50000
)
[description] => Customer X
)
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 14642
)
[user] => Array
(
[0] => SimpleXMLElement Object
(
[description] => Customer Y
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 24151
)
[user] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 1075159
[default-loc] => 50000
[status] => 1
[license] => high,low
[password] => aebecf880f2060c31e44f820785e0aa63ea4cc44
[primary] => 51412
[username] => 17980_51412
)
[description] => Customer Z
)
)
)
)
I am not sure how much this will help you, but when trying to iterate through an XML. I find its best to use recursion instead of looping. Your question doesn't show if you are using a recursive function or a loop but you do say "When looping through..."
If you solve this recursively, you can simply stop and perform your required operation whenever you detect you are at a leaf node that is of type attribute.
This should eliminate any potential null pointer exceptions you may encounter when simply "looping" through the XML.

PHP - Changing an Object for Consistency

I have a scenario where an API is returning multiple records inside object containing a numeric array like so;
stdClass Object
(
[Event] => Array
(
[0] => stdClass Object
(
[ID] => 111
[Name] => My First Event
[EventType] => stdClass Object
(
[ID] => 1
[Category] => Music
)
)
[1] => stdClass Object
(
[ID] => 222
[Name] => My Second Event
[EventType] => stdClass Object
(
[ID] => 2
[Category] => Sport
)
)
)
[Errors] => stdClass Object
(
[Result] => 0
[Message] =>
)
[RecordCount] => 2
)
I'm current using a foreach loop to iterate through the records. This works fine.
foreach($result->Event as $Event)
But there is a problem here I have a scenario where a single results is returned in the object like so;
stdClass Object
(
[Event] => stdClass Object
(
[ID] => 11
[Name] => My Only Event
[EventType] => stdClass Object
(
[ID] => 2
[Category] => Sport
)
)
[Errors] => stdClass Object
(
[Result] => 0
[Message] =>
)
[RecordCount] => 1
)
Notice there is no [0] array index for the single results.
What's the best way to overcome this keeping in mind that I have no control of the data returned by the API?
Check if Event is an array or an object
if( is_object( $result->Event ) )
{
// ...
}
else
{
foreach( // [....]
}
You may process the object or overwrite it with a 1 item array as suggested by Sam
Btw: very bad API design. I would complain....
The best workaround I have found is to add the single Event to an array with a zero index within the result object. This way the result object matches the same structure as a result containing multiple records.
if(!is_array($result->Event)){
$result->Event = array($result->Event);
}

PHP Printing JSON value

I have a JSON array that I want to be able to drill down to a lower level and print just that value. The problem occurs when I reach a level that has is indacted as [0] (or [n]). For example I have the following output, and I want to just print the game key for the first league.
This is how I am trying to print it
HtmlSpecialChars(print_r($user->fantasy_content->users[0]->user[1]->games[0]->game[0]->game_key,1))
However I keep getting this error:
Cannot use object of type stdClass as array
When I do it incrementally it seems to fail on this command (so I assume I'm not index correctly):
$user->fantasy_content->users[0]
Here is the output:
stdClass Object
(
[fantasy_content] => stdClass Object
(
[xml:lang] => en-US
[yahoo:uri] => /fantasy/v2/users;use_login=1/games
[users] => stdClass Object
(
[0] => stdClass Object
(
[user] => Array
(
[0] => stdClass Object
(
[guid] => IYEZUHTVBYRLIB3OAQC5WRZPQY
)
[1] => stdClass Object
(
[games] => stdClass Object
(
[0] => stdClass Object
(
[game] => Array
(
[0] => stdClass Object
(
[game_key] => 147
[game_id] => 147
[name] => Baseball
[code] => mlb
[type] => full
[url] => http://baseball.fantasysports.yahoo.com/b1
[season] => 2006
)
)
)
[count] => 1
)
)
)
)
[count] => 1
)
[time] => 52.390813827515ms
[copyright] => Data provided by Yahoo! and STATS, LLC
[refresh_rate] => 60
)
)
For objects you must use the -> syntax and if the key/property name is a number or has other special characters, you will need to use the $object->{'0'} syntax.
The game_key can be retrieved using:
$user->fantasy_content->users->{'0'}->user[1]->games->{'0'}->game[0]->game_key;
You can convert a stdClass object to an array by casting it like so:
<?php
$array = (array) $myObject;
echo json_encode($array);
You can also cast inline:
<?php
echo json_encode((array) $object);

Getting array key(protected)-value from an array inside object (response from RTM-php)?

When using php library for RTM (https://github.com/bartosz-maciaszek/php-rtm), I am getting a response for a particular tasks-list like this:
Rtm\DataContainer Object
(
[attributes:Rtm\DataContainer:private] => Array
(
[0] => Rtm\DataContainer Object
(
[attributes:Rtm\DataContainer:private] => Array
(
[id] => 19594773
[taskseries] => Rtm\DataContainer Object
(
[attributes:Rtm\DataContainer:private] => Array
(
[id] => 310899576
[created] => 2013-10-03T05:35:52Z
[modified] => 2013-11-06T17:24:36Z
[name] => A new task
[source] => js
[url] =>
[location_id] =>
)
)
)
)
)
)
I want to get the value of [name]. How can I do it?
As far as I could understand from the docs, you have to call something like
$receivedObject->getTaskSeries()->getName()
Or there is a suggestion to apply toArray or toJson to returned object - that should work.

Categories