PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
The key()
function in PHP is used to fetch a key from an array element. This function will return the index element of the current array position.
Here's a basic tutorial on how to use it:
Step 1: Create an array
First, you need to create an array. An array is a special variable that allows you to store multiple values in a single variable.
$array = array( "fruit1" => "Apple", "fruit2" => "Banana", "fruit3" => "Cherry", );
In this array, "fruit1", "fruit2", and "fruit3" are the keys, and "Apple", "Banana", and "Cherry" are the corresponding values.
Step 2: Use the key() function
Now, you can use the key()
function to get the key of the current array element.
echo key($array);
This code will output: fruit1
The key()
function returns the key of the current array element. When you first create an array, the internal pointer of the array points to the first element. That's why key($array)
returns the first key, fruit1
.
Step 3: Move the array pointer
You can change which element the internal array pointer is pointing to using functions like next()
, prev()
, end()
, and reset()
.
For instance, if you want to get the key of the next array element, you can use the next()
function to move the internal array pointer to the next element, and then call key()
again:
next($array); echo key($array);
This code will output: fruit2
Step 4: Reset the array pointer
If you want to return the internal array pointer to the first element, you can use the reset()
function:
reset($array); echo key($array);
This code will output: fruit1
This is a basic tutorial on how to use the key()
function in PHP. There's a lot more you can do with arrays and the various array functions in PHP, but this should give you a good start.
PHP key()
Function Usage:
key()
function is used to retrieve the key of the current array element.$colors = ['Red' => '#FF0000', 'Green' => '#00FF00', 'Blue' => '#0000FF']; echo key($colors); // Outputs: Red
PHP key()
Array Pointer:
key()
returns the key of the current array element without advancing the internal pointer.$fruits = ['Apple', 'Banana', 'Orange']; echo key($fruits); // Outputs: 0
PHP key()
vs current()
Function:
key()
returns the key, current()
returns the value of the current array element.$colors = ['Red' => '#FF0000', 'Green' => '#00FF00', 'Blue' => '#0000FF']; echo key($colors); // Outputs: Red echo current($colors); // Outputs: #FF0000
PHP key()
Function with Associative Array:
key()
is commonly used with associative arrays to retrieve the current key.$person = ['name' => 'John', 'age' => 25, 'city' => 'New York']; echo key($person); // Outputs: name
PHP key()
Function Manual:
key()
retrieves the key of the current array element and does not move the internal pointer.$numbers = [10, 20, 30]; $currentKey = key($numbers); echo "Current Key: $currentKey"; // Outputs: Current Key: 0
PHP key()
Function Use Cases:
key()
is often used in loops to work with the keys of an array.$colors = ['Red' => '#FF0000', 'Green' => '#00FF00', 'Blue' => '#0000FF']; while ($key = key($colors)) { echo "Key: $key "; next($colors); } // Outputs: Key: Red Key: Green Key: Blue
PHP key()
Function Array End:
key()
can be used to get the last key of an array by moving the internal pointer to the end.$numbers = [1, 2, 3, 4, 5]; end($numbers); // Move pointer to the end echo key($numbers); // Outputs: 4
PHP Iterate Array with key()
:
key()
can be used in loops to iterate through an array and work with keys.$fruits = ['Apple', 'Banana', 'Orange']; foreach ($fruits as $key => $value) { echo "Key: $key, Value: $value "; } // Outputs: Key: 0, Value: Apple Key: 1, Value: Banana Key: 2, Value: Orange