<?php
/**PHP holds a wide variety of ways to get the key-value pair of
* array items. They are shown down below.
* This list is not intended to be exhaustive.
*/
// The sample array
$array = array( "foo" => "bar", "b" => "baz", "c" => false, "d" => 31337 );
/*******************************************************************
* EITHER ...
*******************************************************************/
// Reset internal pointer to the beginning and gain the value
$value = reset( $array );
// Get key
// Another way to get the value when the pointer is at the beginning
/*******************************************************************
* OR ...
*******************************************************************/
// Slice array's first element
// Get key ...
// ... and value
/*******************************************************************
* AND ALSO ...
*******************************************************************/
// Get key ...
// ... and value
/*******************************************************************
* AND NOT TO FORGET THE MOST COMMON ...
*******************************************************************/
// Get key ...
$key = $keys[0];
// PHP >= 5.5 shorthand
// ... and value
$value = $values[0];
// PHP >= 5.5 shorthand
/*******************************************************************
* AND OF COURSE ...
*******************************************************************/
$key = $value = ''; // Not really required (in PHP)
// Get key and value concurrently
foreach( $array as $key => $value )
break; // We already have what we want
/*******************************************************************
* AND YET ANOTHER WAY ...
*******************************************************************/
// If interal pointer is not at the beginning, reset() it beforehand
// then get key and value concurrently
/*******************************************************************
* If you need the value of the first element only and have an array
* with NUMERIC index there is - you know it - one more way to go
*******************************************************************/
list( $value ) = $array; // works with NUMERIC indexes only
?>