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 ~]$
Related
Can't access array but var_dump() sees it.
session_start();
include './entities/Answer.php';
include './entities/Question.php';
include './Database.php';
if (isset($_SESSION["question"])) {
$correct = TRUE;
var_dump($_SESSION["question"]);
var_dump($_SESSION["question"]->answers);
}
} else {
echo "<h2>no question selected.</h2>";
}
var_dump($_SESSION["question"]);
shows:
object(__PHP_Incomplete_Class)#2 (8) {
["__PHP_Incomplete_Class_Name"]=> string(8) "Question"
["id"]=> string(1) "1"
["correct"]=> NULL
["wrong"]=> NULL
["answers"]=> array(3) {
[0]=> object(__PHP_Incomplete_Class)#3 (5) {
["__PHP_Incomplete_Class_Name"]=> string(6) "Answer"
["correct"]=> string(1) "0"
["question_id":"Answer":private]=> NULL
["id"]=> string(1) "1"
["text"]=> string(9) "antwort 1"
}
[1]=> object(__PHP_Incomplete_Class)#4 (5) {
["__PHP_Incomplete_Class_Name"]=> string(6) "Answer"
["correct"]=> string(1) "0"
["question_id":"Answer":private]=> NULL
["id"]=> string(1) "2"
["text"]=> string(5) "ant 2"
}
[2]=> object(__PHP_Incomplete_Class)#5 (5) {
["__PHP_Incomplete_Class_Name"]=> string(6) "Answer"
["correct"]=> string(1) "0"
["question_id":"Answer":private]=> NULL
["id"]=> string(1) "3"
["text"]=> string(5) "ant 3"
}
}
["creator_id"]=> NULL
["categories"]=> array(0) { }
["text"]=> string(17) "eine Test frage ?"
}
but var_dump($_SESSION["question"]->answers); shows NULL.
I don't really understand that.
I got this as error message:
PHP Notice: main(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "Question" of the object you are trying to operate on was loaded before unserialize() gets called or provide a __autoload() function to load the class definition
Thanks for your help.
You need to run session_start() before you try to interact with the session object.
Also, you need to define your object before you call session_start(), or else the PHP's session handler won't know how to deserialise the object. That's why you see __PHP_Incomplete_Class.
EDIT: I found this answer that may help you PHP __PHP_Incomplete_Class Object with my $_SESSION data
I had to place session_start after my class definitions. Thanks to Progrock
include './entities/Answer.php';
include './entities/Question.php';
include './Database.php';
session_start();
I am trying to decode JSON
I am reading the JSON through STOMP. There are different JSON datasets so I need to work out which JSON dataset has come through. I do this by reading its title.
However there is one particular dataset I am having trouble reading
foreach (json_decode($msg->body,true) as $event) {
if(isset($event['schemaLocation'])) {
$schedule_id=($event['schedule']['schedule_start_date']);
$signalling_id=($event['schedule']['schedule_segment']['signalling_id']);
echo $schedule_id;
}
In the above example the isset function works fine and also $schedule_id obtains the right answer
However the $signalling_id gives an error of Undefined index:
Here is a dump of PART of the JSON (Its rather long............).The piece of JSON with the signalling_id is towards the end of the JSON. Any help to get the variable signalling_id much appreciated.
array(7) {
["schemaLocation"]=>
string(72) "http://xml.networkrail.co.uk/ns/2008/Train itm_vstp_cif_messaging_v1.xsd"
["classification"]=>
string(8) "industry"
["timestamp"]=>
string(13) "1410374918000"
["owner"]=>
string(12) "Network Rail"
["originMsgId"]=>
string(47) "2014-09-10T18:48:38-00:00vstp.networkrail.co.uk"
["Sender"]=>
array(3) {
["organisation"]=>
string(12) "Network Rail"
["application"]=>
string(4) "TOPS"
["component"]=>
string(4) "VSTP"
}
["schedule"]=>
array(11) {
["schedule_id"]=>
string(0) ""
["transaction_type"]=>
string(6) "Create"
["schedule_start_date"]=>
string(10) "2014-09-10"
["schedule_end_date"]=>
string(10) "2014-09-10"
["schedule_days_runs"]=>
string(7) "0010000"
["applicable_timetable"]=>
string(1) "N"
["CIF_bank_holiday_running"]=>
string(1) " "
["CIF_train_uid"]=>
string(6) "W64017"
["train_status"]=>
string(1) "1"
["CIF_stp_indicator"]=>
string(1) "O"
["schedule_segment"]=>
array(1) {
[0]=>
array(20) {
["signalling_id"]=>
string(4) "5Y75"
["uic_code"]=>
string(0) ""
["atoc_code"]=>
string(0) ""
["CIF_train_category"]=>
string(2) "EE"
["CIF_headcode"]=>
string(0) ""
["CIF_course_indicator"]=
............................................
schedule_segment is itself an array, so instead of
['schedule']['schedule_segment']['signalling_id']);
that should probably be
['schedule']['schedule_segment'][0]['signalling_id']);
As you can see in the var dump, signalling_id is inside another array. Use:
$signalling_id=($event ['schedule']['schedule_segment'][0]['signalling_id']);
If that one element array with key 0 is not constant throughout, you may need some logic to figure out what it is in each iteration.
Hello awesome programmers!
I am so sorry for the the novice question, however, I am having trouble finding a solution to my problem. I am attempting to run a for each loop through my array passed from my controller, however, the data being outputted is not the same as when I run a var_dump($array). I am thinking perhaps that I need to iterate through this object maybe? However, when I attempt to do so, I get a non-object error.
Controller:
$data['user_details'] = $this->ion_auth->user()->row();
View:
var_dump($user_details);
foreach($user_details as $item){
echo $item['email'];
}
The output of this is : "21nfn4111NR12" but should be roger#peterson.net!
I have also tried the object form:
var_dump($user_details);
foreach($user_details as $item){
echo $item->email;
}
However, it results in error trying to get property of non-object!
When I run the var dump I get the following:
object(stdClass)#21 (17) {
["id"]=>
string(1) "2"
["ip_address"]=>
string(14) "119.132.127.01"
["username"]=>
string(12) "roger petereson"
["password"]=>
string(40) "fdZxF/RQo4nZKmbA5XQlwefbc8f8e5c74899c3d0"
["salt"]=>
NULL
["email"]=>
string(19) "roger#peterson.net"
["activation_code"]=>
NULL
["forgotten_password_code"]=>
NULL
["forgotten_password_time"]=>
NULL
["remember_code"]=>
string(22) "44hjOlloLTIrkSrjSBVNie"
["created_on"]=>
string(10) "1404939094"
["last_login"]=>
string(10) "1405099607"
["active"]=>
string(1) "1"
["first_name"]=>
string(6) "Roger"
["last_name"]=>
string(5) "Peterson"
["is_owner"]=>
string(1) "1"
["user_id"]=>
string(1) "2"
}
The following line:
$this->ion_auth->user()->row();
returns an object not an array (check your var_dump output), so you just need to
echo $user_details->email;
I want to update two variables in my the Woocommerce cart object. I can see the structure of the arrays in the object with:
echo 'Cart Dump: ' . var_dump($woocommerce->session->cart)
This returns:
array(1) { ["01822dd92bc31f60fdb64f0c3c5eb241"]=> array(9) { ["product_id"]=> int(616) ["variation_id"]=> string(0) "" ["variation"]=> string(0) "" ["quantity"]=> int(1) ["addons"]=> array(2) { [0]=> array(3) { ["name"]=> string(5) "Color" ["value"]=> string(13) "Black / Black" ["price"]=> string(0) "" } [1]=> array(3) { ["name"]=> string(8) "Warranty" ["value"]=> string(13) "12 Month Free" ["price"]=> string(0) "" } } ["line_total"]=> float(689) ["line_tax"]=> float(0) ["line_subtotal"]=> float(689) ["line_subtotal_tax"]=> float(0) } } Cart Dump:
I want to be able to set the variables "value" and "price" inside that object.
My next step has been to try to step into the next array and have tried
echo 'Cart Dump: ' . var_dump($woocommerce->session->cart[1])
but I think I am mixing object references with array ones here and I am getting NULL. I can appreciate that I would be better off creating a function for the object but I am not sure how to best approach that if I cannot even access the variable I want to set.
Many thanks in advance.
There are no objects here, it's just that the array key is the string 01822dd92bc31f60fdb64f0c3c5eb241 and not the integer 1.
So to get / set your value, you would use:
$woocommerce->session->cart["01822dd92bc31f60fdb64f0c3c5eb241"]["addons"][0]["value"]
etc.
Depending on your php version you would probably need a temporary variable to get to the first element of your array if you don't know the key:
$temp = reset($woocommerce->session->cart);
$value = $temp["addons"][0]["value"];
You can try this:
//search the key of the array
$cart = $woocommerce->session->cart;
//update values using the key of the array as key
$woocommerce->session->cart[key($cart)]['value'] = 'your value';
$woocommerce->session->cart[key($cart)]['price'] = 'your price';
I have the following already decoded json stored in $response = $result->response;:
object(stdClass)#6 (5) {
["EmailAddress"]=> string(18) "email#gmail.com"
["Name"]=> string(0) ""
["Date"]=> string(19) "2011-10-09 19:32:00"
["State"]=> string(6) "Active"
["CustomFields"]=> array(1) {
[0]=>object(stdClass)#7 (2) {
["Key"]=>string(2) "id"
["Value"]=>string(6) "Dl9lIz"
}
}
I can already access the main attributes (EmailAddress, Name, etc) with:
$email = $response->{'EmailAddress'};
print $email;
But I need to access the "Value" portion in the CustomFields object. I don't know how to dig that deep. I'm attempting to do this in PHP..
Any suggestions?
It is contained in the first element ([0]) of array CustomFields, so you can access it with an object operator (->) after the array index.
print $response->CustomFields[0]->Value;