<?php // 定义一个数组 $array = [ 'a' => 'Apple', 'b' => 'Banana', 'c' => 'Cat' ]; // 获取数组的第一个键firstKey = reset(array_keys($array)); // 输出第一个键 echo $firstKey; // 输出 'a' ?> 在这个示例中,array_keys($array)会返回数组的所有键,...
PHParray_key_first()函数 完整的 PHP Array 参考手册 实例 获取数组的第一个键值: <?php$array=['a'=>1,'b'=>2,'c'=>3];$firstKey=array_key_first($array);echo$firstKey;?> 执行以上代码,输出结果为: a 定义和用法 array_key_first() 函数用于获取指定数组的第一个键值。
1. 使用array_keys()函数获取所有键,并取得第一个键: “`php $array = array(“one” => 1, “two” => 2, “three” => 3); $keys = array_keys($array); $firstKey = $keys[0]; “` 2. 使用array_key_first()函数获取第一个键(PHP版本> = 7.3): “`php $array = array(“one” ...
The PHP array_key_first() function returns the first key of the given array without affecting the internal array pointer. Syntax array_key_first(array) Parameters array Required. Specify an array. Return Value Returns the first key of array if the array is not empty; null otherwise. ...
$firstKey = $keys[0]; echo $firstKey; “` 2. 使用reset()函数: “`php $array = array(“key1” => “value1”, “key2” => “value2”, “key3” => “value3”); $firstKey = reset($array); echo $firstKey; “` 以上两种方式都可以输出数组的第一个键值,可以根据具体需求选择其中...
";// 输出:fooecho$last_key." ";// 输出:baz AI代码助手复制代码 如上所示,使用array_key_first()函数可以轻松地获取数组的第一个键名,并将其存储在变量$first_key中。同样,使用array_key_last()函数可以获取数组的最后一个键名,并将其存储在变量$last_key中。
array_key_first— Gets the first key of an arrayОпис ¶ array_key_first(array $array): int|string|null Get the first key of the given array without affecting the internal array pointer. Параметри ¶ array An array. З...
Parameters array An array. Return Values Returns the first key of array if the array is not empty; null otherwise. Examples Example #1 Basic array_key_first() Usage <?php $array = ['a' => 1, 'b' => 2, 'c' => 3]; $firstKey = array_key_first($array); var_dump($firstKey...
<?php $mixed = [ 10 => 'ten', 'color' => 'blue', 20 => 'twenty' ]; $firstKey = array_key_first($mixed); echo "First key: "; var_dump($firstKey); Despite having string keys later, the function returns the first key (10). The type (int) is preserved in the return ...
echo “First key: ” . $firstKey . “\n”; echo “Last key: ” . $lastKey . “\n”; ?> “` 输出结果为: “` First key: apple Last key: orange “` 上面的示例中,我们分别使用array_key_first()和array_key_last()函数获取了数组的第一个键和最后一个键,并通过echo语句将它们输出。