简体   繁体   English

如何获取数组的最后一个值?

[英]How can I get the last value of an array?

Below are the contents of the array. 以下是数组的内容。 How can I get the last array, [cat_name] => Lcd ? 如何获得最后一个数组[cat_name] => Lcd

I do not need: 我不需要:

[cat_link] => lcd 
[cat_id] => 172

(
    [0] => Array
        (
            [cat_name] => Electronics
            [cat_link] => electronics
            [cat_id] => 164
        )

    [1] => Array
        (
            [cat_name] => Televisions
            [cat_link] => televisions
            [cat_id] => 165
        )

    [2] => Array
        (
            [cat_name] => Lcd
            [cat_link] => lcd
            [cat_id] => 172
        )

)

Use end() . 使用end()

$lastMember = end($arr);

Note that this advances the array's internal pointer, which may matter dependent on what other code you have. 请注意,这会提高数组的内部指针,这可能取决于您拥有的其他代码。

您可以像这样检索数组的最后一个元素: $array[count($array) - 1]

您可以这样获得最后一个cat_name值:

$arr[count($arr) - 1]['cat_name']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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