PHP array_key_last() function

The array_key_last () function in PHP gets the last key of an array if the specified array is not empty.

Syntax

array_key_last ( array $array ) 

Parameter

array(required)- This parameter signifies the input array.

Return

This function returns the last key of the specified array if the array is not empty else it returns NULL.

Example 1


$array = ['first' => 1, 'second' => 2, 'third' => 3,'fourth' => 4];
echo("Array : \n");
var_export($array);
//will return the last key for $array
$lastKey = array_key_last($array);
//printing the returned value
echo("\nThe last key of the array is ");
var_dump($lastKey);
?> 

Output

Array : 
array (
  'first' => 1,
  'second' => 2,
  'third' => 3,
  'fourth' => 4,
)
The last key of the array is string(6) "fourth" 

Example 2

 96, 'Harshit' => 94, 'Sapna' => 93];
echo("Student Array : \n");
var_export($contestent_array);
//will return the last key for $array
$lastKey = array_key_last($contestent_array);
//printing the returned value
echo("\nThe II runner up is ");
var_dump($lastKey);
//removing the last element of the array
array_pop($contestent_array);
echo("\nThe I runner up is ");
var_dump(array_key_last($contestent_array));
//removing the last element of the array
array_pop($contestent_array);
echo("\nThe winner of the match is ");
var_dump(array_key_last($contestent_array));
?> 

Output

Student Array: 
array (
  'Reema' => 96,
  'Harshit' => 94,
  'Sapna' => 93,
)
The II runner up is string(5) "Sapna"
The I runner up is string(7) "Harshit"
The winner of the match is string(5) "Reema" 

Example 3

 

Output

Array: 
'The first key of the array is 0' 

Example 4

'Reema',1 => 'Harshit',2 =>'Sapna'];
echo("Student Array : \n");
var_export($array);
//will return the last key for $array
$lastKey = array_key_last($array);
//printing the returned value
echo("\nThe last element is ");
var_dump($lastKey);
//adding elements at the end of the array
array_push($array,"Amar","Sukla");
echo("\nNew Array: ");
var_export($array);
echo("\nThe last element is ".array_key_last($array));
?> 

Output

Student Array : 
array (
  0 => 'Reema',
  1 => 'Harshit',
  2 => 'Sapna',
)
The last element is int(2)
New Array: array (
  0 => 'Reema',
  1 => 'Harshit',
  2 => 'Sapna',
  3 => 'Amar',
  4 => 'Sukla',
)
The last element is 4 

Related Topics

PHP Example

We can create a simple program by writing the source code inside the php tag and save it with .php extension. Syntax: <?php // your code here ?> Let us take an example of PHP. <!DOCTYPE html> <html> <head>            ...

1 minute read.

PHP str_getcsv() Function

PHP str_getcsv() Function The str_getcsv() function in PHP parses a CSV string into an array and returns an array containing the fields read. Syntax str_getcsv ( string $input [, string $delimiter [, string $enclosure [, string$escape ]]] ) Parameter input(required)- This parameter represents the string to parse. delimiter(optional)- This parameter signifies...

1 minute read.

PHP array_diff_ukey() Function

The array_diff_ukey() function in PHP compares the keys from array1 against the keys from array2 and returns the difference containing all the entries from array1 that are not present in any of the other arrays. Syntax array_diff_ukey ( array $array1 , array $array2 [, array $... ], callable $key_compare_func ) Parameter array1(required)- This parameter signifies the...

1 minute read.

PHP sizeof() Function

PHP sizeof() Function The sizeof() function in counts all elements and return the size of the array. This function is an alias of count(). Syntax sizeof( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )  Parameter array_or_countable(required)– This parameter represents the input array or countable object. mode(optional)-...

1 minute read.

PHP str_rot13() Function

PHP str_rot13() Function The str_rot13() function in PHP performs the ROT13 encoding on a string and returns the resulting string. The ROT13 encoding is used to shift every letter by 13 places in the alphabet...

1 minute read.

PHP array_combine() Function

PHP array_combine() Function The array_combine() function in PHP is used to create an array by combining two arrays i.e., one array for keys and another array for its values. Syntax array_combine ( array $keys , array $values ) Parameter keys(required)- This parameter represents an array which acts...

1 minute read.

PHP key() Function

PHP key() Function The key() function in PHP fetches a key from an array and returns the index element of the current array position. Syntax key ( array $array ) Parameter array(required)- This parameter represents the input array. Return This function returns the key of the...

1 minute read.

PHP Continue

What is continue in PHP? Inside the loop, the continue statement is used to control the loop in preprocessor hypertext. PHP utilizes the continue keyword to implement the continue statement, which...

3 minutes read.

PHP For Loops

PHP for loop is used when the specified condition is predefined. Syntax: for (initialization; condition; increment/decrement ) { code to be executed; } Example <!DOCTYPE html> <html> <head> <title>PHP Tutorial</title> </head> <body> <?php echo " Print counting using for loop"."<br>"; for($i=1;$i<=5;$i++){ echo $i."<br>"; } ?> </body> </html> Output Print counting using...

1 minute read.

PHP str_word_count() Function

PHP str_word_count() Function The str_word_count() function in PHP counts the number of words inside the string and returns the information about words used in a string. String str_word_count( string $string [, int $format [, string $charlist ]] ) Parameter string(required)- This parameter represents the input string. format(required)- This parameterspecifies the...

2 minutes read.

PHP arsort() function

PHP arsort() function The arsort() function in PHP sorts an array in the reverse order and maintains the index association Syntax arsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) Parameter array(required)- This parameter represents the input array. sort_flags (optional)- This parameter is used to modify the behavior...

2 minutes read.

PHP strtoupper() Function

The strtoupper() function in PHP is used to convert a string to uppercase. This function is binary-safe. The related functions are as follows: strtolower()lcfirst()ucfirst()ucwords() Syntax strtoupper ( string $string ) Parameter string(required)- This parameter represents the input string. Return This function returns a string with all alphabetic...

1 minute read.

PHP stripos() Function

PHP stripos() Function The stripos() function in PHP finds the position of the first occurrence of a substring in the specified string. It is a case-insensitive a binary-safe function. The related functions are as follows: strripos() Functionstrpos() Functionstrrpos() Function Syntax stripos ( string $haystack , mixed $needle [, int $offset...

1 minute read.

PHP str_replace() Function

PHP str_replace() Function The str_replace() function in PHP replaces all occurrences of the search string with the specified replacement string. This function is case-sensitive. Syntax str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) Parameter search(required)- This parameter signifies the value to search. replace(required)-...

1 minute read.

PHP break

What is Break in PHP? Inside the loop, the break statement is used to control the loop in preprocessor hypertext. PHP utilizes the break keyword to implement the break statement, which...

3 minutes read.

PHP is_numeric() Function

The is_numeric() function in PHP finds whether the specified variable is a number or a numeric string. It returns a Boolean value TRUE if the specified var is a number or a numeric string else it returns FALSE otherwise. Syntax is_numeric ( mixed...

1 minute read.

PHP is_resource() Function

PHP is_resource() Function The is_resource() function in PHP finds whether a variable is a resource or not. It returns a Boolean value TRUE if the parameter var is resource, else it returns FALSE Syntax is_resource ( mixed $var ) Parameter var(required)- This parameter represents the value to check. Return This function...

1 minute read.

PHP print() Function

PHP print() Function The print() function in PHP is used to output one or more string. Syntax print ( string $arg ) Parameter arg(required)- This parameter represents the input data. It can take one or more arguments. Return This function returns an integer value 1. Example...

1 minute read.

WordPress Plugin

WordPress Plugin is a process used to add or extend the functionality which can be enabled or disabled, and it does not interfere with the software and its code. WordPress is lightweight and smooth...

2 minutes read.

PHP print_r() Function

PHP print_r() Function The print_r() function in PHP displays the information stored in a variable. Syntax print_r (mixed $expression [, bool $return] ) Parameter expression(required)- This parameter represents the expression to be printed. return(optional)- This parameter is used to store the output of...

2 minutes read.