如何在不使用循环的情况下获取关联数组的特定字段


How to get specific field of an associative array without using a loop?

我想获取数组的特定字段,但它不断向我发送错误,例如Undefined index: inCannot use object of type stdClass as array in。我正在使用代码点火器,这个数组来自我的模型,它在我的控制器中返回。这是我的控制器。

  $data['inpatient'] = $this->billing_model->retrieveinpatient();
  echo $data['inpatient']['in'];

这是我的数组的结构:

 Array
 (
  [inpatient] => Array
    (
        [0] => stdClass Object
            (
                [iid] => 1
                [in] => IN
            )
    )
)

有什么帮助吗?

看看你的结构。数组输出是键 => 值。您的第一个阵列具有键inpatientinpatient值是一个数组,其中有一个键 - 0,保存 stdClass。

所以一定是

echo $data['inpatient'][0]->in;

问题是您正在尝试使用数组点表示法访问对象。

试试这个:

echo $data['inpatient'][0]->in;

$data['inpatient']确实是一个数组,因此您可以使用索引表示法。在我的示例中,索引是 0 。指向正确的索引后,您将有一个可以使用stdClass Object。为了提取stdClass Object的属性,你可以使用对象属性表示法->来获取in属性。