简体   繁体   中英

Grabbing the item in the last array element

So I have an array of items and I want to grab the number item in the last array, I need the able to grab it in the last item, I feel like this is pretty simple but I tried using the end() function and it do not seem to work, here is the example array:

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
    )

 )

So im trying to only grab [number] from the last array item in the array

end() is the right function but you also need to get the property value as its an array of objects not an array of arrays.

echo end($array)->number;

or

$item = end($array);
echo $item->number;

让我们假设数组存储在一个名为 $array 的变量中:

echo $array[sizeof($array) - 1)]->number;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM