在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
)
以上是幾種常用的方法來獲取數組鍵值的方式。可以根據具體需求選擇適合的方法。