Getting stdclass and array values in PHP [duplicate] - php

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

Related

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

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

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.

How do I acces this property/element of this object/array? [duplicate]

This question already has answers here:
How to access object properties with names like integers or invalid property names?
(7 answers)
Closed 6 years ago.
I'm trying to access a property of a stdClass but it's named after an index or something? I'm at a loss here, I would really appreciate some insight into how I can get at my data.
Thanks in advance,
object(stdClass)#79 (96) {
["0"]=> <------this is what I'm trying to access
array(1) {
[0]=>
object(stdClass)#80 (5) {
["m_id"]=>
string(3) "422"
["klplan_id"]=>
string(3) "945"
["m_naam"]=>
string(62) "opsporen lekkage vanuit badkamer/kitten bad rand /parkeren/kit"
["m_aantal"]=>
string(1) "1"
["m_prijs"]=>
string(4) "7.25"
}
}
This should do it:
$class->{'0'}

How get a value of my array in php? [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 7 years ago.
This is my array:
array(3) {
["formData"]=>
array(25) {
["Contact.Name.First"]=>
object(stdClass)#17 (2) {
["value"]=>
string(31) "POLIANA KRUSCHER PISCOLLE"
["required"]=>
bool(true)
}
["Contact.CustomFields.c.new_cpf"]=>
object(stdClass)#21 (2) {
["value"]=>
string(14) "038.889.971-99"
["required"]=>
bool(true)
}
}
How can I retrieve value in Contact.CustomFields.c.new_cpf?
I tried $incident_data['Contact.CustomFields.c.new_cpf']['value'], but it returns null.
Try this way:
$incident_data['formData']['Contact.CustomFields.c.new_cpf']->value;
Your $incident_data['formData']['Contact.CustomFields.c.new_cpf'] does not contain an array yet you try to access it as such.
Since there is no such key php defaults to null. (Yet there also should be an undefined index notice thrown around. You may want to enhance your error logging / strictness level).
As by your dump you have an of the \stdClass class, you have to treat them as such:
$object->value

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