PHP 中的嵌套变量

发布于 2024-11-28 06:47:38 字数 1578 浏览 1 评论 0原文

我想我要么只是愚蠢,要么就是什么,但我仍然无法理解他们。

我试图从这个变量 $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

硬不硬你别怂 2024-12-05 06:47:38
$object=$result['intervention'][0];
print $object->patient_age;

检查是否有其他变量可访问

$object=$result['intervention'][0];
print $object->patient_age;

Check if any other variables are accessable

探春 2024-12-05 06:47:38

这应该是你的最后一个例子。这并不难,真的。 $result 是一个包含单个元素的数组,其键为“intervention”。您可以使用 [ 和 ] 访问数组的元素。因此,使用 $result['intervention'],您会得到一个还包含单个元素的数组:键 0 处的元素,它是 stdClass 的实例。您可以通过使用 $result['intervention'][0] 来实现这一点。如果您想从该 stdClass 获取 Patient_age,您可以使用 -> 访问实例变量。因此,这应该可行:

echo $result['intervention'][0]->patient_age;

以下内容将导致 $ Patient 成为 stdClass 实例,然后您可以从中检索 Patient_age :

$patient = $result['intervention'][0];
echo $patient->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:

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;
心如荒岛 2024-12-05 06:47:38

$结果[0]->病人年龄;

*只要 Patient_age 是公共变量*就可以工作。如果它是私有的或受保护的,您将需要使用对象中的方法来访问它。

你从来没有说过你尝试过的东西发生了什么。空值?错误/警告消息?

证明:

<?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 ~]$

$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 ~]$
貪欢 2024-12-05 06:47:38

快速解答 (TL;DR)

  • 使用 JMESPATH 可以轻松、干净地访问深度嵌套的 PHP 变量

详细答案

上下文

  • PHP 版本 7.x、8.x
  • PHP 插件库 JMESPATH
  • 处理复杂的嵌套 PHP 变量

问题

  • 用例场景: 开发人员希望访问嵌套 PHP 变量的特定部分,
  • 代码应该最少且易于理解

解决方案

  • 使用 PHP 的 JMESPATH 插件库
  • 使用 JMESPATH 表达式来访问和转换嵌套PHP 变量

基本示例

  • 从 PHP 变量访问值的基本示例 PHP 代码

     需要 'vendor/autoload.php';
        $lookup = '@.intervention[0]. Patient_age';
        $结果=数组(
            “干预”=>大批(
                大批(
                    “部门_id”=> “1”,
                    “部门名称”=> 《心脏病学》,
                    “剂量_吸收”=>无效的,
                    “剂量_发射”=>无效的,
                    “剂量_id”=> 'uu247pushuprag1652203549',
                    “剂量_周期”=>无效的,
                    “持续时间”=> “02:26:00”,
                    “电子文档”=> "布鲁斯1.jpg",
                    “干预_评论”=>无效的,
                    “干预_描述”=> "",
                    “intervention_id”=> “1”,
                    “干预名称”=> “IVH 2”,
                    “intervention_name_id” => “1”,
                    “名称_id”=> “1”,
                    “患者年龄”=> “34”,
                    “患者_性别”=> “男性”,
                    “患者身高”=> “1.34”,
                    “患者 ID” => “1”,
                    “患者体重”=> “45”,
                    “已提交”=> “2011-07-31 19:56:29”,
                    “用户id” => “0”
                )
            )
        );
        JmesPath\search($lookup, $result);
    
      
    <块引用>
    

    '34'

优点

  • 使用统一的最小语法处理复杂变量,
  • 避免使用循环和临时变量来存储数据

陷阱

  • 需要安装默认情况下 PHP 不附带的 JMESPATH 插件
  • 需要熟悉不属于本机 PHP 的 JMESPATH 表达式规范

另请参阅

Quick Answer (TL;DR)

  • Access to deeply nested PHP variables can be done easily and cleanly with JMESPATH

Detailed Answer

Context

  • PHP version 7.x, 8.x
  • PHP addon library JMESPATH
  • dealing with complex nested PHP variables

Problem

  • Use-case scenario: Developer wishes to access specific part(s) of a nested PHP variable
  • the code should be minimal and easy to understand

Solution

  • use the JMESPATH addon library for PHP
  • use JMESPATH expressions to both access and transform nested PHP variables

Basic Example

  • Basic Example PHP code to access a value from a PHP variable

        require 'vendor/autoload.php';
        $lookup = '@.intervention[0].patient_age';
        $result = array(
            "intervention" => array(
                array(
                    "department_id" => "1",
                    "department_name" => "Cardiology",
                    "dosage_absorbed" => null,
                    "dosage_emitted" => null,
                    "dosage_id" => 'uu247pushuprag1652203549',
                    "dosage_period" => null,
                    "duration" => "02:26:00",
                    "edocument" => "Bruce1.jpg",
                    "intervention_comment" => null,
                    "intervention_description" => "",
                    "intervention_id" => "1",
                    "intervention_name" => "IVH 2",
                    "intervention_name_id" => "1",
                    "name_id" => "1",
                    "patient_age" => "34",
                    "patient_gender" => "Male",
                    "patient_height" => "1.34",
                    "patient_id" => "1",
                    "patient_weight" => "45",
                    "submitted" => "2011-07-31 19:56:29",
                    "user_id" => "0"
                )
            )
        );
        JmesPath\search($lookup, $result);
    
      

    '34'

Advantages

  • process complex variables with uniform minimal syntax
  • avoids having to use loops and temporary variables to store off data

Pitfalls

  • requires installation of JMESPATH addon which does not come with PHP by default
  • requires familiarity with JMESPATH expressions which are not part of the native PHP spec

See also

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文