How to search for highest key/index in an array?

0 votes

How can I get with PHP the highest key/index in an array? 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 program it, but I was asking myself if there as a function for this as well.

Oct 6 in PHP by kartik
• 36,020 points
9 views

1 answer to this question.

0 votes

Hello @kartik,

This should work fine

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

Hope it helps!!
Thank you!!

answered Oct 6 by Niroj
• 78,480 points

Related Questions In PHP

0 votes
1 answer

How to pass an array via $_GET in php?

Hii, You can pass an associative array to http_build_query() and ...READ MORE

answered 4 days ago in PHP by Niroj
• 78,480 points
17 views
0 votes
1 answer

How to pass an array within a query string?

Hello, Submitting multi-value form fields, i.e. submitting arrays ...READ MORE

answered Apr 7 in PHP by Niroj
• 78,480 points
3,088 views
0 votes
1 answer

How to put an 'if clause' in an SQL string?

Hello @kartik, For your specific query, you can ...READ MORE

answered Jun 1 in PHP by Niroj
• 78,480 points
82 views
0 votes
1 answer

Error:Jquery - Uncaught TypeError: Cannot use 'in' operator to search for '324' in

Hello @kartik, You have a JSON string, not ...READ MORE

answered Jun 16 in PHP by Niroj
• 78,480 points
948 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 78,480 points
3,526 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18 in Laravel by Niroj
• 78,480 points
421 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23 in Laravel by Niroj
• 78,480 points
292 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23 in Laravel by Niroj
• 78,480 points
4,228 views
0 votes
1 answer

How to remove duplicate values from an array in PHP?

Hello @kartik, Use array_unique(): Example: $array = array(1, 2, 2, 3); $array ...READ MORE

answered Sep 15 in PHP by Niroj
• 78,480 points
65 views
0 votes
1 answer

How to delete element by value in array (not key) using php?

Hello @kartik, Using array_search() and unset, try the following: if (($key = ...READ MORE

answered Sep 16 in PHP by Niroj
• 78,480 points
37 views