Pulling apart stdclass with PHP - php

I'm trying to access part of an StdClass that has a property #text. PHP uses '#' for comments so I'm having trouble getting PHP to parse it not as a comment. An example of the StdClass is below
stdClass Object
(
[artist] => stdClass Object
(
[name] => John Denver
[mbid] => 34e10b51-b5c6-4bc1-b70e-f05f141eda1e
[url] => http://www.last.fm/music/John+Denver
[image] => Array
(
[0] => stdClass Object
(
[#text] => http://userserve-ak.last.fm/serve/34/521025.jpg
[size] => small
and I've tried to access it with:
$json->artist->image[0]->#text
but how can I escape the '#' or tell php to interpret it differently. Or is there another format to find the #text property.
I also tried:
$json['artist']['image'][0]['#text']
but I get an error. I'm sure this is something simple but it's really got me at the moment.

You can access such properties with the following code:
$object->{'#text'}

It seems to be JSON data, so when decoding, you can also set the second parameter to true:
$json = json_decode($input, true);
Now you are able to use this:
$json['artist']['image'][0]['#text']

Citing another answer you can put the key in a variable an accessing it via this variable.
$var='myvarwithstrangecharacters$asdfas#';
$object->$var;//do whatever you want
from:
How do I access a PHP object attribute having a dollar sign?

Related

Having trouble extracting data from PDO array

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;

escape 'value' in OO PHP

I was not sure how to title this question feel free to re-word it...
I am dealing with a SOAP response that has an object by the name of 'value'(I know... not smart it is third party so I cannot change it!)
$application = $result->return[$i]->extendedist[12]->value->displayValue;
[value] => stdClass Object
(
[displayValue] =>
[internal] =>
[id] =>
)
As you can see this is a problem because PHP wants to use its built in value function. How can I escape this and use the value object from the response?
$result->{'return'}
Should work.
Depending on how you read the response you could decode it into an array instead and use
$result['return']
You can access an object property such as $foo->value as $foo->{'value'} as well.

Fields get unwantedly concatenated in Salesforce SOQL query result. Developer nearly loses it

I'm probably missing something quite basic, but I'm getting very confused (and frustrated) with the results I get from my SOQL queries to the Salesforce API.
My query:
Select Id, FirstName, LastName FROM contact
The resulting object (as rendered by print_r):
stdClass Object
(
[done] => 1
[queryLocator] =>
[records] => Array
(
[0] => stdClass Object
(
[type] => Contact
[Id] => Array
(
[0] => 0032000000cPd7uAAC
[1] => 0032000000cPd7uAAC
)
[any] => BuzzAldrin
)
[1] => stdClass Object
(
[type] => Contact
[Id] => Array
(
[0] => 0032000000cPt1zABC
[1] => 0032000000cPt1zABC
)
[any] => RonnieVanZant
)
[2] => stdClass Object
(
[type] => Contact
[Id] => Array
(
[0] => 0032000000cPb60AA
[1] => 0032000000cPb60AA
)
[any] => PollyJeanHarvey
)
)
[size] => 3
)
The first thing I don't get is why "Id" is an array. A strange quirk, but a workaround is not too hard.
The second thing bothers me endlessly more, though: I select for FirstName and LastName and what happens is they get concatenated and returned as a single string value for a field called "any". To avoid the "split it on uppercase letters" advice I already got from my colleagues, I provided an example with both a two-capital first name and a two-capital last name, and anyhow, in reality I need many more (and more formally unpredictable) fields, and they all get added to this "any" property.
Does anyone see what I'm doing wrong? Assuming it's not such a badly written API, that is?
Edit:
Said developer will now go sit in a corner for a few hours, repenting for not having checked for more recent versions of PHP Toolkit. Seems I was using 11.0, whereas there's already a version 20.0. Shame on me, shame on me indeed. Sorry for wasting your time.
The behavior you are seeing is mostly because of how PHP's SoapClient interprets the results from the API. If you call getLastResponse() on your API connection after you make the query() calls above, you'll see what the actual SOAP messages look like coming back from Salesforce.
As far as the Id array -- its not really an array, but it is listed twice per record (once for the record itself and once as a field), but PHP turns it into an array because it sees it twice. As far as the any, that's happening because PHP is not understanding the namespaced field tags correctly.
As it looks like you found, using the PHP Toolkit can help with these oddities and return sensible objects for you to work with. You might also want to consider looking at Salesforce's REST API, whose results can be directly consumed by json_decode(). For making the HTTP calls to Salesfore, you might be interested in this simple (almost standalone) REST client in a project of mine.

Parsing php array in python

I'm getting a PHP array from a web page (as a string).
It looks like :
Array
(
[k1] => Array
(
[a] => Array
(
[id] => 1
[age] => 60
)
[b] => Array
(
[id] => 2
[age] => 30
)
)
[k2] => v2
)
I want to parse it in python.
Does anyone have an idea how to do this?
Thanks,
Rivka
Edit:
This is really not a json, like a few people commented.
Thanks for the comments, and I updated the question.
That's not JSON, that's just how PHP prints arrays. If you want to create JSON of the array, check out json_encode for PHP. Then use Python's JSON library (or here for py3) to read it.
If I understood you correctly, you are using print_r on array to get that output. This is a visual representation of array only, you can't really parse it. For example:
array('Array'.PHP_EOL."\t(".PHP_EOL." [0] => test".PHP_EOL."\t)")
will look exactly like
array(array('test'));
You should use some real serializing function to do what you want(json,serialize etc.);

Working with Delicious API json response

When I try to find out the total count of delicious bookmarks, Delicious returns a json file which when decoded is:
Array (
[0] => stdClass Object (
[hash] => e60558db2d649c8a1933d50f9e5b199a
[title] => ISRAEL: Thousands march in Jerusalem rally, Israel still kicking
new families out of land they've owned for 60+ years
[url] => http://english.aljazeera.net/news/middleeast/2010/03/2010362141312196.html
[total_posts] => 2
[top_tags] => Array ()
)
)
The array is a stdClass Object. How can I extract [total_count] using PHP.
P.S Since I could not figure out how to extract it, I am using strpos to find 'total_count' and then I am cutting the string from that position and then extracting integers out of it. :)
BUt it is way too long.
Solved
If you pass a second argument, true, in your json_decode, it will return a regular array as opposed to an object. You might find this easier to work with.
$array = json_decode($json, true);
If you pass a second argument, true, in your json_decode, it will return a regular array as opposed to an object. You might find this easier to work with.
$array = json_decode($json, true);
Have you tried
echo $stdClass->total_posts;
Do you mean [total_posts]? If so, you should use $delArray[0]->total_posts.
See http://us.php.net/manual/en/language.types.object.php for samples on accessing object properties.

Categories