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

问题描述:

Below are the contents of the array. How can I get the last array, [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
        )

)

以下是数组的内容。 如何获取最后一个数组, [cat_name] => Lcd code>? p>

我不需要: p>

  [cat_link] =>  lcd 
 [cat_id] =>  172 
  code>  pre> 
 
 
 (
 [0] =>数组
(
 [\ t 
] [cat_name] =>电子 
 [cat_link] => electronics 
 [cat_id] => 164 
)
 
 [1] =>数组
(
 [cat_name] =>电视
 [cat_link] =  >电视
 [cat_id] => 165 
)
 
 [2] =>数组
(
 [cat_name] => Lcd 
 [cat_link] => lcd 
 [  cat_id] => 172 
)
 
)
  code>  pre> 
  div>

You can get the last cat_name value like so:

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

You can retrieve the last element of the array like this: $array[count($array) - 1]

Use end().

$lastMember = end($arr);

Note that this advances the array's internal pointer, which may matter dependent on what other code you have.