开发者

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"
      ["in开发者_JAVA技巧tervention_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 ~]$
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜