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.
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.
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.
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.
This should work fine
$arr = array( 1 => "A", 10 => "B", 5 => "C" ); max(array_keys($arr));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With