2

Possible Duplicate:
How to get last key in an array?

I have an array

$arr=Array
  (
   [0] = Array
       (
           [groupid] = 1
           [groupname] = Oxy
           [members] = Array
               (
                   [0] = Array
                       (
                           [id] = 9
                           [name] => Anith
                       )

                   [1] = Array
                       (
                           [id] = 12
                           [name] = sanjay
                       )

                   [3] =Array
                       (
                           [id] = 13
                           [name] = Sooraj K
                       )

               )

       ) )

Here $arr[0]['members'][2] is unsetted. I want to find the last index of $arr[0]['members'] here that is 3..how can i find this last index

Community
  • 1
  • 1
Sanu Saji
  • 137
  • 2
  • 9

5 Answers5

4
$last = end($arr[0]['members']);
deyes
  • 637
  • 6
  • 9
1

This will work for you.

end($arr[0]["members"]);
$key = key($arr[0]["members"]);

It will return you 3.

anuj arora
  • 831
  • 12
  • 22
-1

You can simply count the array, i.e

$count = count($arr[0]['members']);

This will give total number of index. Then to fetch it, you can go like

$last_array = $arr[0]['members'][$count-1];

I also suggest to also check that it is greater that 0 or not.

anuj arora
  • 831
  • 12
  • 22
-2

Do like :

<?php

$arr_keys = array_keys($arr[0]['members']);
$last_index = $arr_keys[count($arr_keys)-1];

?>
-2

$last = max(array_keys($arr[0]['members']));

Of course, this means sorted array.

biakaveron
  • 5,493
  • 1
  • 16
  • 20