我有以下数组
$country["china"] = array(
"shortcode" => "CN",
"president" => "Xi Jinping",
"currency" => "Yuan",
);
$country["america"] = array(
"shortcode" => "US",
"president" => "Barack Obama",
"currency" => "Dollar",
);
$country["russia"] = array(
"shortcode" => "RU",
"president" => "Vladimir Putin",
"currency" => "Rubble",
);
现在我想提取上面每个数组的“总统”值。
我试过这个
foreach(array_keys($country) as $countryName => $countryvalue)
echo $countryvalue . "<br>";
我希望获得此输出 习近平 巴拉克奥巴马 弗拉基米尔·普京
但我获得了一份国家名单。
答案 0 :(得分:4)
试试这个
foreach($country as $c)
{
echo $c["president"]."<br/>";
}
答案 1 :(得分:1)
foreach($country as $countryName => $countryvalue){
echo $val["president"];
}
答案 2 :(得分:1)
试试这个
$country["china"] = array(
"shortcode" => "CN",
"president" => "Xi Jinping",
"currency" => "Yuan",
);
$country["america"] = array(
"shortcode" => "US",
"president" => "Barack Obama",
"currency" => "Dollar",
);
$country["russia"] = array(
"shortcode" => "RU",
"president" => "Vladimir Putin",
"currency" => "Rubble",
);
foreach($country as $key=>$val){
echo $val["president"];
}
答案 3 :(得分:1)
试试这个
foreach($country as $key => $value) {
echo $value['president']."<br/>";
}
答案 4 :(得分:0)
我强烈建议您将所有这些内容放入数据库并使用简单查询访问数据。
// Your table
CREATE TABLE `country`
(
id serial primary key
, name varchar(64)
, shortcode varchar(2)
, president varchar(64)
, currency varchar(24)
);
// Insert data
INSERT INTO `country`
( name, shortcode, president, currency )
VALUES
( 'china', 'CN', 'Xi Jinping', 'Yuan' )
, ( 'america', 'US', 'Barack Obama', 'Dollar' )
, ( 'russia', 'RU', 'Vladimir Putin', 'Rubble' );
现在要访问,只需编写简单的查询:
SELECT president FROM country WHERE name = 'china';
PS:通过这种方式更新数据会更容易,您无需更改代码。