所以我有一个项目数组,我想抓住最后一个数组中的数字项,我需要能够在最后一项中抓住它,我觉得这很简单,但我尝试使用结束()函数,它似乎不起作用,这是示例数组:
Array
(
[0] => stdClass Object
(
[ID] => 1
[number] => 1
[mode] => 1
[timestamp] => 2018-03-20 15:23:58
[question_text] => Hello
)
[1] => stdClass Object
(
[ID] => 2
[number] => 2
[mode] => 1
[timestamp] => 2018-03-20 15:23:58
[question_text] => Hello 2
)
[2] => stdClass Object
(
[ID] => 3
[number] => 3
[mode] => 1
[timestamp] => 2018-03-20 15:23:58
[question_text] => Hello 3
)
[3] => stdClass Object
(
[ID] => 4
[number] => 4
[mode] => 1
[timestamp] => 2018-03-20 15:23:58
[question_text] => Hello 4
)
)
所以我试图只从数组中的最后一个数组项中获取[number]
答案 0 :(得分:0)
end()
是正确的函数,但您还需要将属性值作为一个对象数组而不是数组数组。
echo end($array)->number;
或
$item = end($array);
echo $item->number;
答案 1 :(得分:-1)
假设数组存储在名为$ array:
的变量中echo $array[sizeof($array) - 1)]->number;