如何在 PHP 中从数组中获取单个值
主题:PHP / MySQL
答案:使用数组键或索引
如果您想从索引、关联或多维数组中访问单个值,您可以通过使用数组索引或键来完成。
让我们看看下面的例子来了解它的基本工作原理:
示例
<?php
/* Indexed array */
$sports = array("Baseball", "Cricket", "Football", "Shooting");
/* Associative array */
$cities = array("France"=>"Paris", "India"=>"Mumbai", "UK"=>"London", "USA"=>"New York");
/* Multidimensional array */
$superheroes = array(
array(
"name" => "Peter Parker",
"character" => "Spider-Man",
),
array(
"name" => "Tony Stark",
"character" => "Iron-Man",
),
array(
"name" => "Clark Kent",
"character" => "Super-Man",
)
);
echo $sports[0]; /* Outputs: Baseball */
echo "<br>";
echo $sports[1]; /* Outputs: Cricket */
echo "<br>";
echo $cities["France"]; /* Outputs: Paris */
echo "<br>";
echo $cities["USA"]; /* Outputs: New York */
echo "<br>";
echo $superheroes[0]["name"]; /* Outputs: Peter Parker */
echo "<br>";
echo $superheroes[1]["character"]; /* Outputs: Iron-Man */
?>
FAQ 相关问题解答
以下是与此主题相关的更多常见问题解答: