PHP array_key_first() function

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

Syntax

array_key_first(array;$array)

Parameter

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

Return

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

Example 1

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

Output

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

Example 2

 96, 'Harshit' => 94, 'Sapna' => 93,'Chandhan' => 92];
echo("Student Array : \n");
var_export($student_array);
//will return the first key for $array
$firstKey = array_key_first($student_array);
//printing the returned value
echo("\nThe topper of the class is ");
var_dump($firstKey);
?> 

Output

Student Array: 
array (
  'Reema' => 96,
  'Harshit' => 94,
  'Sapna' => 93,
  'Chandhan' => 92,
)
The topper of the class is string(5) "Reema" 

Example 3

 

Output

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

Example 4

'Reema',4 => 'Harshit',3 =>'Sapna'];
echo("Student Array : \n");
var_export($array);
//will return the last key for $array
$firstKey = array_key_first($array);
//printing the returned value
echo("\nThe first element is ");
var_export($firstKey);
// sorting the keys of the arrays in ascending order
ksort($array);
echo("\nNew Array: ");
var_export($array);
echo("\nThe first element is ".array_key_first($array));
?> 

Output

Student Array : 
array (
  9 => 'Reema',
  4 => 'Harshit',
  3 => 'Sapna',
)
The first element is 9
New Array: array (
  3 => 'Sapna',
  4 => 'Harshit',
  9 => 'Reema',
)
The first element is 3 

Related Topics

PHP is_array() Function

The is_array() Function in PHP is used to  find whether a variable is an array or not. It returns a Boolean value true if the parameter var is an array else it returns...

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.

Python main() function

What is a main() function? In most of the programming languages, there is this one special function i.e., main() which is executed firstly everytime when we run the program. The special...

6 minutes read.

PHP strnatcasecmp() Function

PHP strnatcasecmp() Function The strnatcasecmp() function in PHP compares two strings using a "natural" algorithm. It is a case-insensitive function. Syntax strnatcasecmp ( string $str1 , string $str2 )  Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter represents the second string. Return This function returns a value less...

1 minute read.

PHP end() function

PHP end() function The end() function in PHP sets the internal pointer of an array to its last element. This function returns the value of the last element Syntax end ( array &$array )   Parameter array (required)– This parameter represents the input array. Return This function returns...

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 str_ireplace() Function

PHP str_ireplace() Function The PHP str_ireplace() function in PHP replaces some characters with some other characters in a string. Syntax str_ireplace ( mixed $search , mixed  $replace , mixed $subject [, int &$count ] ) Parameter search(required)- This parameter specifies the value being searched for. replace(required)- This parameter represents the replacement value...

1 minute read.

PHP compact() function

PHP compact() function The compact() function in PHP is used to create an array containing variables and their values. It returns the output array whose keys are the variable names, and their corresponding values are...

1 minute read.

PHP foreach Loop

PHP foreach Loop with Example PHP foreach loop is used to loop through the associative arrays. Syntax: foreach($array as $key => $value) { //Statement } $array = associative array. $key = array key. &value =...

2 minutes read.

PHP doubleval() Function

PHP doubleval() Function The doubleval() function in PHP is used to give the float value of a variable. This function is an alias of floatval(). Syntax doubleval ( mixed $var ) Parameter var(required)- This parameter represents the name of the variable that...

1 minute read.

PHP array_intersect_uassoc() function

The array_intersect_uassoc () function in PHP is used to compute the intersection of arrays with the help of an additional index check, comparing indexes by a callable function. It returns an array containing...

1 minute read.

PHP array_intersect_assoc() function

PHP array_intersect_assoc() function The array_intersect_assoc() function in PHP is used to compute the intersection of arrays with the help of an additional index check. It returns an array containing all the entries from array1 that are present...

1 minute read.

PHP Associative Arrays

PHP associate array is used to access the elements with keys by “=>” symbol. There are two ways to create an associative array. 1st way: $age = array("John"=>"20", "Helena"=>"30", "Rasmus"=>"29"); 2nd way: $age['john'] = "20"; $age['Helena'] =...

1 minute read.

PHP array() function

PHP array() function The array() function in PHP is used to create an array. Syntax array ([ mixed $... ] ) Parameter …(optional)- This parameter represents the elements’ values along with keys of the array. The syntax of this is as follows: "index => values"...

1 minute read.

PHP levenshtein() Function

PHP levenshtein() Function The levenshtein() function in PHP calculates the Levenshtein distance between two strings. Syntax levenshtein ( string $str1 , string $str2 , int $cost_ins , int $cost_rep , int $cost_del )  Parameter str1(required)- This parameter signifies one of the strings being evaluated for Levenshtein distance. str2(required)- This parameter signifies one of the strings being evaluated...

1 minute read.

PHP prev() Function

PHP prev() Function The prev() function in PHP rewinds or moves back the internal array pointer one place backward and returns the prev array value. It behaves like next() function, except that this function rewinds the...

1 minute read.

PHP array_diff_uassoc() Function

PHP array_diff_uassoc() Function The array_diff_uassoc () Function in PHP is used to compare the keys and values of two (or more) arrays and returns the differences (an array containing the entries from the first array that...

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.

Python if-else

Python if-else The else statement is associated with if statement. An else statement executes if the conditional expression in if the statement becomes false or a Zero value. If the conditional expression...

2 minutes read.

PHP strnatcmp() Function

PHP strnatcmp() Function The strnatcmp() function in PHP compares two strings using a "natural" algorithm. It is case-sensitive. Syntax strnatcmp ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter represents the second string. Return This function returns a value less than(...

1 minute read.