1

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 ) ) 

3 Answers 3

3

Use end().

$lastMember = end($arr); 

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

Sign up to request clarification or add additional context in comments.

Comments

2

You can get the last cat_name value like so:

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

1 Comment

@baris22: PHP 5.2 and lower have this odd quirk where [ cannot follow ). So end($arr)['cat_name'] would be a syntax error. Just FYI :)
2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.