Accesing specific array in php [duplicate] - php

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;

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

How to echo an array in PHP? and display particular value array vale [duplicate]

This question already has answers here:
PHP how to retrieve array values
(3 answers)
Closed 4 years ago.
array(11) { ["statusCode"]=> string(2) "OK" ["statusMessage"]=> string(0) "" ["ipAddress"]=> string(13) "183.82.100.13" ["countryCode"]=> string(2) "IN" ["countryName"]=> string(5) "India" ["regionName"]=> string(9) "Telangana" ["cityName"]=> string(9) "Hyderabad" ["zipCode"]=> string(6) "500018" ["latitude"]=> string(7) "17.3753" ["longitude"]=> string(7) "78.4744" ["timeZone"]=> string(6) "+05:30" }
i want to display zipcode only
By the look of the output you might have used the var_dump statement. Any array item can be accessed in the below manner:
echo $array['zipCode'];
Not quite sure how your code looks but if $array is that array you have printed use
echo $array["zipCode"];
You can learn more about how arrays work here http://php.net/manual/en/language.types.array.php
Please try to do your own research before posting a question though as this is a fairly standard part of the language

How to access array within PHP object [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
I know this question is a little n00b but I am struggling to work out to how to access the array 'ta[]' within the PHP object below. Usually I would have no problem but because my key contains the brackets i.e. 'ta[]' I can't wrap my head around how to access it, I guess I somehow need to escape it..?
I have tried most combinations such as..
object->ta[]
object["ta[0]"]
object["ta[]"]
object->ta[0]
Any help welcome!
object(stdClass)#6 (11) {
["tc"]=> string(4) "4500"
["tct"]=> string(1) "1"
["pd"]=> string(2) "AT"
["df"]=> string(10) "08/04/2016"
["dt"]=> string(10) "08/08/2016"
["nt"]=> string(1) "2"
["ta[]"]=> array(2)
{
[0]=> string(2) "40"
[1]=> string(2) "35"
}
["rc"]=> string(2) "US"
["rs"]=> string(2) "AR"
["cc"]=> string(2) "US"
["dfp"]=> string(10) "07/30/2016"
}
This should do it
$obj->{"ta[]"};
Brace notation (using {}) does the same thing for accessing object properties as bracket notation (using []) does for accessing array keys: It lets you define the property name as an expression.
Which in this case is just a simple string but could could be any other expression. To prove that with a (silly) example:
function ta() {
return 'ta';
}
$obj = new stdClass;
$obj->{ta() . '[]'} = ['a', 'b'];
echo $obj->{"ta[]"}[1]; // b

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

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

Categories