【问题标题】:Retrieve value from associative array-error从关联数组错误中检索值
【发布时间】:2022-01-27 11:15:01
【问题描述】:

你好

我偶然发现了一个简单的问题,但我不知道可能出了什么问题:

        function zabbixGraphGetId($HostName, $Name,$zabbixData) {

            try {

                $api = new ZabbixApi(...$zabbixData);

            } catch (Exception $e) {
                // Exception in ZabbixApi catched
                echo $e->getMessage();
            }
            $GetGraphId = $api->graphGet(array(
                'output' => 'extend',
                'filter' => array('host' => $HostName),
                'search' => array('name' => $Name)
            ));
            $ReturnGraphId = $GetGraphId['0']->graphid;

            return $ReturnGraphId;
        }

这就是我在 symfony 中的函数,我想从数组 $GetGraphId 中获取只有 graphid 的值。要点是在我调用此函数后出现此错误:警告:尝试读取数组上的属性“graphid” 但如果我返回整个数组($GetGraphId),我会得到这个:

array:2 [
  0 => array:19 [
    "graphid" => "some id"
    "name" => "Network traffic on tun0"
    "width" => "900"
    "height" => "200"
    "yaxismin" => "0"
    "yaxismax" => "100"
    "templateid" => "0"
    "show_work_period" => "1"
    "show_triggers" => "1"
    "graphtype" => "0"
    "show_legend" => "1"
    "show_3d" => "0"
    "percent_left" => "0"
    "percent_right" => "0"
    "ymin_type" => "1"
    "ymax_type" => "0"
    "ymin_itemid" => "0"
    "ymax_itemid" => "0"
    "flags" => "4"
  ]

所以有graphid值但我仍然无法得到它,我真的很感激任何线索/想法/解决方案 谢谢!

【问题讨论】:

  • 警告说:尝试读取数组上的属性“graphid”。您不能使用 object->property 表示法访问 graphid。改用这一行:$ReturnGraphId = $GetGraphId['0']['graphid'];
  • 很好解决了我的问题xd。我无法将评论标记为解决方案。你会回答这个问题吗,我把它标记为已解决。谢谢!

标签: php arrays associative-array zabbix zabbix-api


【解决方案1】:

警告说:尝试读取数组上的属性“graphid”。您无法使用 object->property 表示法访问 graphid。改用这一行:

$ReturnGraphId = $GetGraphId['0']['graphid']; 

【讨论】:

  • $GetGraphId[0]['graphid'];更好,如果键数组是数字而不是字符串,则使用数字访问索引
猜你喜欢
  • 2017-06-09
  • 2015-06-03
  • 1970-01-01
  • 2011-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-27
  • 2018-10-02
相关资源
最近更新 更多