How to access array within PHP object [duplicate] - php

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

Related

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

Extract a string from an object inside an array: PHP [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 5 years ago.
I'm writing a PHP plugin and need to extract a string from an object which is inside an array.
var_dump($data)
outputs:
array(1) {
[0]=> object(stdClass)#380 (53) {
["id"]=> string(1) "2"
["firstname"]=> string(6) "John"
["lastname"]=> string(6) "Doe"
["email"]=> string(31) "johndoe#email.com"
}
}
I want to return:
johndoe#mail.com
My research has turned up functions such as unserialize, array_map, array_slice, and array_column, but I haven't found the right function or combination of functions that work together (nor do I understand them enough) to just return a simple string from an object inside an array. Can anyone provide me with some guidance?
Edit: This isn't the same question as the one referenced as "the exact same question." The other one asks simply how to access an array (answer: $array[0]), and my question asked how to access an object INSIDE an array (answer: $array[0]->text).
For example:
$data[0]->firstname
Maybe:
$arr = get_object_vars($data[0]);
$arr['firstname'];
Example:
$obj = (object) array('x' => 'foo');
$arr = [$obj];
print($arr[0]->x);
You will get: foo

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

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;

Nested Variables in PHP

I think I am either just stupid or something but I still can't get my head around them.
I am trying to access "patient_age" from this variable $result.
Here is the var dump.
array(1) {
["intervention"]=>
array(1) {
[0]=>
object(stdClass)#23 (21) {
["intervention_id"]=>
string(1) "1"
["patient_id"]=>
string(1) "1"
["name_id"]=>
string(1) "1"
["department_id"]=>
string(1) "1"
["dosage_id"]=>
NULL
["edocument"]=>
string(10) "Bruce1.jpg"
["user_id"]=>
string(1) "0"
["duration"]=>
string(8) "02:26:00"
["submitted"]=>
string(19) "2011-07-31 19:56:29"
["intervention_comment"]=>
NULL
["patient_age"]=>
string(2) "34"
["patient_height"]=>
string(4) "1.34"
["patient_weight"]=>
string(2) "45"
["patient_gender"]=>
string(4) "Male"
["department_name"]=>
string(10) "Cardiology"
["intervention_name_id"]=>
string(1) "1"
["intervention_name"]=>
string(5) "IVH 2"
["intervention_description"]=>
string(0) ""
["dosage_emitted"]=>
NULL
["dosage_absorbed"]=>
NULL
["dosage_period"]=>
NULL
}
}
}
I have tried :
$result[0]->patient_age;
$result[1]->patient_age;
$result['intervention']->patient_age;
$result['intervention'][0]->patient_age;
Hopefully someone could give me the answer but also explain how they came to this answer as all the other Stackoverflow questions they just give the solution but not the method.
Anyone got any tips how to navigate nested variables.
Thanks
$object=$result['intervention'][0];
print $object->patient_age;
Check if any other variables are accessable
It should be your last example. It's not that hard, really. $result is an array which contains a single element, with the key "intervention". You can access an array's elements by using [ and ]. So, with $result['intervention'], you get an array that also contains a single element: the element at key 0, which is an instance of stdClass. You can reach that by using $result['intervention'][0]. If you want to get the patient_age from that stdClass, you can access the instance variables with the ->. So, this should work:
echo $result['intervention'][0]->patient_age;
The following would result in $patient being the stdClass instance, which you can then retrieve patient_age from:
$patient = $result['intervention'][0];
echo $patient->patient_age;
$result[0]->patient_age;
will work *as long as patient_age is a public variable*. If it's private or protected you'll need to use a method within the object to access it.
You never said what happened with the stuff you tried. Null values? Error/warning messages?
Proof:
<?php
class iv {
var $patient_age;
function __construct($val)
{
$this->patient_age=$val;
}
}
$t=new iv(40);
$result=array(0=>$t);
var_dump($result) . "\n\n";
print "val = " . $result[0]->patient_age . "\n\n";
[user#example ~]$ php -q t.php
array(1) {
[0]=>
object(iv)#1 (1) {
["patient_age"]=>
int(40)
}
}
val = 40
[user#example ~]$

Categories