Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search for highest key/index in an array

How can I get the highest key/index in an array with php? I know how to do it for the values.

E.g.: from this array I would like to get 10 as an integer value:

$arr = array(1 => "A", 10 => "B", 5 => "C"); 

I know how I could code it but I was asking myself if there is a function for this as well.

like image 875
Raffael Luthiger Avatar asked May 25 '11 14:05

Raffael Luthiger


People also ask

How do you find the largest element in an array with index and value?

To find the largest element, the first two elements of array are checked and the largest of these two elements are placed in arr[0] the first and third elements are checked and largest of these two elements is placed in arr[0] . this process continues until the first and last elements are checked.

How do you find array keys?

The array_keys() function is used to get all the keys or a subset of the keys of an array. Note: If the optional search_key_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned. Specified array.

How do you search an array?

Use filter if you want to find all items in an array that meet a specific condition. Use find if you want to check if that at least one item meets a specific condition. Use includes if you want to check if an array contains a particular value. Use indexOf if you want to find the index of a particular item in an array.


1 Answers

This should work fine

$arr = array( 1 => "A", 10 => "B", 5 => "C" ); max(array_keys($arr)); 
like image 59
Gérald Croës Avatar answered Oct 03 '22 02:10

Gérald Croës