PHP

PHP Menu

PHP

key() Function - Definition, Syntax, Parameters, Examples

Definition

The key() function returns the element key from the current internal pointer position, returns false on error.

The key() function is commonly used along with the following functions:

  • current() – Returns the value of the current element in an array.
  • next() – Moves the internal pointer of an array to the next element, and returns its value.
  • prev() – Moves the internal pointer of an array to the previous element, and returns its value.
  • end() – Moves the internal pointer of an array to its last element, and returns its value.
  • reset() – Set the internal pointer of an array to its first element, and returns its value.

Syntax

key(array)

Parameters

Parameter Description
array Required. Specifies the array to work on.

Example

<?php
$cities = array("New York", "Salt Lake", "Tokyo"); echo key($cities); echo "<br>";
// Examples in conjunction with other related functions $ages = array("Mark" => 22, "Jeff" => 32, "Mike" => 28);
echo current($ages) . "<br>"; echo next($ages) . "<br>"; echo current($ages) . "<br>"; echo prev($ages) . "<br>"; echo end($ages) . "<br>"; echo prev($ages) . "<br>"; echo current($ages) . "<br>"; echo reset($ages) . "<br>"; echo next($ages) . "<br>";

Introduction

PHP Basics

PHP Advance

PHP OOP

PHP Functions and Methods