How do I access a specific key in this PHP object? [duplicate] - php

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
I have the following code:
$response = $client->get_json($query);
var_dump($response);
which shows me this in a browser:
object(stdClass)#8 (7) {
["search_metadata"]=>
object(stdClass)#7 (8) {
["id"]=>
string(24) "5fa06b463ffd1f75b14c1b98"
["status"]=>
...
}
["search_parameters"]=>
object(stdClass)#9 (9) {
["engine"]=>
string(6) "google"
["q"]=>
...
}
["inline_images"]=>
array(10) {
[0]=>
object(stdClass)#11 (2) {
["link"]=>
string(231) "/search?q=796714619071&num=30&gl=us&hl=en&tbm=isch&source=iu&ictx=1&fir=iYsIHfZTBqNwZM%252Cfl2S346slZj2tM%252C_&vet=1&usg=AI4_-kRkH4vMP2OKxQ8Mz6SJlNoImL7gPg&sa=X&ved=2ahUKEwiCzK_n2OTsAhUKWa0KHQauCKkQ9QF6BAgHEAY#imgrc=iYsIHfZTBqNwZM"
["thumbnail"]=>
....
I'm trying to access inline_images with the following PHP code:
$inlineImages = $response['inline_images'];
But whenever I add this line I'm getting a HTTP 500 error. What am I doing wrong? Why can't I see to access inline_images? Commenting the line out makes the page appear properly.

It's a property on an object, so:
$inlineImages = $response->inline_images;
That's an array of objects, so you can loop through them like this:
foreach ($inlineImages as $inlineImage) {
$link = $inlineImage->link;
}

Related

PHP Legacy Support, stdClass issue

I was recently handed some of our companies legacy PHP code to work with. There have been updates to the WSDL and API and PHP version (from 5.3 to 5.6) used with this product since the code worked and the code no longer works properly. Specifically;
$response = $mySforceConnection->query($query);
//var_dump($response);
$out='<table><tr><th>Spec Name</th><th>Name</th><th>SF Number</th><th>MW</th></tr>';
foreach ($response->records as $record) {
$out.='<tr><td>'.$record->fields->Spec->fields->Name.'</td>';
$out.='<td>'.$record->fields->Spec->fields->Owner->fields->FirstName.' ';
$out.=$record->fields->Spec->fields->Owner->fields->LastName.'</td>';
$out.='<td>'.$record->fields->Spec->fields->Owner->fields->EmployeeNumber.'</td>';
$out.='</table>';
exit($out);}
Error out with;
[13-Apr-2017 09:42:52 US/Eastern] PHP Notice: Undefined property: stdClass::$fields in T:\sample.php on line 44
[13-Apr-2017 09:42:52 US/Eastern] PHP Notice: Trying to get property of non-object in T:\sample.php on line 44
The $record returned is;
object(stdClass)#8 (3) {
["type"]=>
string(19) "SpecLineItem"
["Id"]=>
array(2) {
[0]=>
string(18) "00k6000000QX8E7AAL"
[1]=>
string(18) "00k6000000QX8E7AAL"
}
["any"]=>
array(1) {
["Spec"]=>
object(stdClass)#9 (3) {
["type"]=>
string(11) "Spec"
["Id"]=>
NULL
["any"]=>
array(2) {
[0]=>
string(74) "<sf:Name>Machineworks - National-Renewal-2015-01-01</sf:Name>"
["Owner"]=>
&object(stdClass)#10 (3) {
["type"]=>
string(4) "User"
["Id"]=>
NULL
["any"]=>
string(119) "<sf:EmployeeNumber>12345</sf:EmployeeNumber><sf:FirstName>John</sf:FirstName><sf:LastName>Doe</sf:LastName>"
}
}
}
}
}
Afraid this falls outside of my very limited PHP scope. How do I adjust the references in the $out construct to properly reference the data? Sorry for something that is likely so basic a question.

Json and php: How can I parse this array (object) [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
I have this JSON response:
object(stdClass)#1 (2) {
["data"]=>
array(47) {
[0]=>
object(stdClass)#2 (4) {
["id"]=>
int(341)
["competition_id"]=>
int(1)
["name"]=>
string(9) "2015/2016"
["active"]=>
bool(true)
But I don't know how to parse the data.
I tried it with this PHP code:
echo $json->data[0]->id;
But it doesn't work. How can I get the ID for example?
here is my solution:
$json = file_get_contents($url);
$json2 = json_decode($json);
echo $json2->data[0]->id;
sorry for complication, the goal was the json_decode() function, after that I can get the data with "->"
greetings!

Cannot get value in associative array [duplicate]

This question already has an answer here:
Why can't I access the array with index directly?
(1 answer)
Closed 6 years ago.
I have an array which var_dump($array) produces
array(7)
{
["*attributes"]=> array(4)
{
["type"]=> string(6) "hidden"
["name"]=> string(3) "hmo"
["class"]=> string(12) "form-control"
["id"]=> string(3) "hmo"
}
["*label"]=> NULL
["*labelAttributes"]=> array(0) { }
["*labelOptions"]=> array(0) { }
["*messages"]=> array(0) { }
["*options"]=> array(1)
{
["disable_inarray_validator"]=> bool(true)
}
["*value"]=> string(243) "{"My-Office":{"Floor":"New - ","Walls":"New - ","Door":"New - ","Switches":"New - ","Table":"New - ","Chair":"New - "},"Other office":{"Floor":"New - ","Walls":"New - ","Door":"New - ","Switches":"New - ","Table":"New - ","Chair":"New - "}}"
}
I am trying to access the json string in the last position (*value) but I cannot access it using $array['*value'] as I get nothing returned. If I var_dump($array['*value']) I get NULL. Has anyone any idea why $array['*value'] does not give me the string I require?
Use
array_values(array_slice($array, -1))[0];
to access the last element of the array $array.

Getting stdclass and array values in PHP [duplicate]

This question already has answers here:
Return PHP object by index number (not name)
(5 answers)
Closed 7 years ago.
How do I access the following stdclass, and echo Transaction ID has already been used.
object(stdClass)#2 (1) {
["SendBulkSMS_PHPResult"]=>
object(stdClass)#3 (1) {
["string"]=>
array(3) {
[0]=>
string(1) "0"
[1]=>
string(37) "Transaction ID has already been used."
[2]=>
NULL
}
}
}
From the look of it, it would be:
$object->SendBulkSMS_PHPResult->string[1];

Accesing specific array in php [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 7 years ago.
I have the following array printed out with var_dump($myArray):
array(2) {
[0]=> object(stdClass)#2 (4) {
["date"]=> string(25) "2015-07-17T05:31:49+02:00"
["author"]=> string(2) "Me"
["subject"]=> string(9) "MySubject"
["message"]=> string(19) "This is my message."
}
["message"]=> string(4) "test"
}
I want to print out only the subject. I tried
echo $myArray[0]["subject"];
but I get an empty site.
Thanks for your help.
As your dump explained ... its an object
so you can access it via echo $myArray[0]->subject;

Categories