PHP 中的嵌套变量
我想我要么只是愚蠢,要么就是什么,但我仍然无法理解他们。
我试图从这个变量 $result 访问“patent_age”。
这是 var 转储。
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
}
}
}
我已经尝试过了:
$result[0]->patient_age;
$result[1]->patient_age;
$result['intervention']->patient_age;
$result['intervention'][0]->patient_age;
希望有人能给我答案,同时也解释一下他们是如何得出这个答案的,因为所有其他 Stackoverflow 问题他们只是给出了解决方案,但没有给出方法。
任何人都可以获得如何导航嵌套变量的任何提示。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
检查是否有其他变量可访问
Check if any other variables are accessable
这应该是你的最后一个例子。这并不难,真的。 $result 是一个包含单个元素的数组,其键为“intervention”。您可以使用 [ 和 ] 访问数组的元素。因此,使用 $result['intervention'],您会得到一个还包含单个元素的数组:键 0 处的元素,它是 stdClass 的实例。您可以通过使用 $result['intervention'][0] 来实现这一点。如果您想从该 stdClass 获取 Patient_age,您可以使用 -> 访问实例变量。因此,这应该可行:
以下内容将导致 $ Patient 成为 stdClass 实例,然后您可以从中检索 Patient_age :
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:
The following would result in $patient being the stdClass instance, which you can then retrieve patient_age from:
*只要 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:
快速解答 (TL;DR)
详细答案
上下文
问题
解决方案
基本示例
从 PHP 变量访问值的基本示例 PHP 代码
优点
陷阱
另请参阅
Quick Answer (TL;DR)
Detailed Answer
Context
Problem
Solution
Basic Example
Basic Example PHP code to access a value from a PHP variable
Advantages
Pitfalls
See also