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

PHP gettype() Function The gettype () function in PHP gets the type of the specified variable. Syntax gettype ( mixed $var ) Parameter var(required)- This parameter represents the variable being type checked. Return This function returns the type of the PHP variable specified by “var”...

1 minute read.

PHP var_export() Function

PHP var_export() Function The var_export() function in PHP is used to get the structured information for the specified variable. This function outputs or returns a parsable string representation of a variable. This function is similar to...

1 minute read.

PHP array_uintersect() Function

PHP array_uintersect() Function The array_uintersect() function in PHP calculates the intersection of arrays by comparing the data by a callback function. It returns an array containing all the values of the first input array that are...

2 minutes read.

PHP Comment

PHP commentis a line of code that is not executed. It is often used by developer to understand the code. PHP support single and multiple comments. In other language like C,...

1 minute read.

PHP array_values() Function

PHP array_values() Function The array_values () function in PHP returns all the values from the array and indexes the array numerically. Syntax array_values ( array $array ) Parameter array(required)- This array represents the input array. Return This function returns an indexed array of values. Example 1 Output Input Array: Array ...

1 minute read.

PHP Constant

PHP constant is an identifier(name) for a simple value. The value does not change during the script. It starts with a letter or underscore not $ sign. The define()function is...

1 minute read.

PHP key_exists() Function

PHP key_exists() Function The key_exists() function in PHP checks if the specified key or index exists in the array or not. Syntax key_exists ( mixed $key , array $array )  Parameter key(required)- This parameter represents the value to check. array(required)- This parameter signifies an input array with...

1 minute read.

PHP crc32() Function

PHP crc32() Function The crc32() function in PHP  Calculates the crc32 polynomial of a string. Syntax crc32( string $str ) Parameter str(required)- This parameter represents The string to be calculated Return This function returns the crc32 checksum of string as an integer. Example 1 ...

1 minute 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_change_key_case() Function

PHP array_change_key_case() Function The array_change_key_case() function in PHP is used to change the case of all keys present in the specified array. It returns an array with all keys in the specified array to lowercase or...

1 minute read.

PHP sprintf() Function

PHP sprintf() Function The sprintf() function in PHP is used to write a formatted string to a variable and returns a formatted string. PHP 4 and above versions support the sprintf() function. The related functions are...

8 minutes read.

PHP empty() Function

PHP empty() Function The empty() function in PHP determines whether the specified variable is empty or not. The specified variable is considered empty if it does not exist or if its value is equal to FALSE. This function...

1 minute read.

PHP Forms

PHP form is used to take input from users, by using superglobals$_REQUEST, $_GET and $_POSTmethod. There are various request methods in PHP Get method Post method Put method Patch method Delete...

5 minutes read.

Composer Installation

Composer is an application-level package manager for the PHP Programming language. The development began in April 2011 and it was first released on March 1, 2012. Commands: require install update remove Installation:  Download the installer by visiting...

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

PHP substr_replace() Function The substr_replace() function in PHP replaces a part of a string with another string. Syntax substr_replace (mixed $string , mixed $replacement , mixed $start [, mixed $length ] ) Parameter string(required)- This parameter represents the input string. replacement(required)- This parameter signifies the string to replace with. start(required)-...

2 minutes read.

PHP Echo And Print

In PHP, echo and print is used to get output data to the screen but there is some difference, between echo and print. PHP echo PHP echo is used to display the...

1 minute read.

PHP bin2hex() Function | Convert Binary to Hexadecimal

PHP bin2hex() Function The bin2hex() function in PHP converts the specified string value of ASCII characters to hexadecimal value. Syntax bin2hex(str) Parameter str(Required): This parameter represents the string to be converted into hexadecimal. Return  This function returns...

1 minute read.

PHP isset() function

PHP isset is used to check whether the variable is set or not.The PHP isset() function returns false if variable contains a NULL value. Syntax: isset(variable1, variable2….) Presentation of PHP isset: In...

2 minutes read.

PHP ord() Function

PHP ord() Function The ord() function in PHP converts the first byte of a string to a value between 0 and 255. This function interprets the binary value of the first byte of string as an...

1 minute read.