// First itemlist($firstItem) = $yourArray;
// First item from an array that is returned from a functionlist($firstItem) = functionThatReturnsArray();
// Second itemlist( , $secondItem) = $yourArray;
<?php// Get the first element of this array.$array = array( 4 => 'apple', 7 => 'orange', 13 => 'plum' );
// Gets the first element by key$result = $array[4];
// Expected result: string appleassert('$result === "apple" /* Expected result: string apple. */');?>
解决方案2-array_flip()+key()
<?php// Get first element of this array. Expected result: string apple$array = array( 4 => 'apple', 7 => 'orange', 13 => 'plum' );
// Turn values to keys$array = array_flip($array);
// You might thrown a reset in just to make sure// that the array pointer is at the first element.// Also, reset returns the first element.// reset($myArray);
// Return the first key$firstKey = key($array);
assert('$firstKey === "apple" /* Expected result: string apple. */');?>
<?php$original = array(4 => array('one', 'two'), 7 => array('three', 'four'));reset($original); // to reset the internal array pointer...$first_element = current($original); // get the current element...?>
$array = array( 4 => 'apple', 7 => 'orange', 13 => 'plum' );echo $array[key($array)];
// key($array) -> will return the first key (which is 4 in this example)
/*** A quick and dirty way to determine whether the passed in array is associative or not, assuming that either:<br/>* <br/>* 1) All the keys are strings - i.e. associative<br/>* or<br/>* 2) All the keys are numeric - i.e. not associative<br/>** @param array $objects* @return boolean*/private function isAssociativeArray(array $objects){// This isn't true in the general case, but it's a close enough (and quick) approximation for the context in// which we're using it.
reset($objects);return count($objects) > 0 && is_string(key($objects));}
$myArray = array (4 => 'apple', 7 => 'orange', 13 => 'plum');$arrayKeys = array_keys($myArray);
// The first element of your array is:echo $myArray[$arrayKeys[0]];
$array = array('step one', 'step two', 'step three', 'step four');
// by default, the pointer is on the first elementecho current($array) . "<br />\n"; // "step one"
//Forward the array pointer and then reset it
// skip two stepsnext($array);next($array);echo current($array) . "<br />\n"; // "step three"
// reset pointer, start again on step oneecho reset($array) . "<br />\n"; // "step one"