在PHP中,有几种方法可以获取数组的键值:
$array = ["a" => 1, "b" => 2, "c" => 3];
$keys = array_keys($array);
print_r($keys);
输出:
Array
(
[0] => a
[1] => b
[2] => c
)
$array = ["a" => 1, "b" => 2, "c" => 3];
foreach($array as $key => $value) {
echo $key . "\n";
}
输出:
a
b
c
$array = ["a" => 1, "b" => 2, "c" => 3];
$keys = array_keys($array);
$values = array_values($array);
print_r($keys);
print_r($values);
输出:
Array
(
[0] => a
[1] => b
[2] => c
)
Array
(
[0] => 1
[1] => 2
[2] => 3
)
以上是几种常用的方法来获取数组键值的方式。可以根据具体需求选择适合的方法。