PHP 数组键值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3722484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:51:59 来源:igfitidea点击:
PHP array keys values
提问by Mark Lalor
I have an arraylike this
我有一个这样的数组
$data = array(
"some" => "163",
"rand" => "630",
"om" => "43",
"words" => "924",
"as" => "4",
"keys" => "54"
);
How can I get each set's keyassociated with their valueslike this:
我怎样才能让每个集合的键与它们的值相关联,如下所示:
foreach ($data as $stuff){
$this->$stuff["key"] = $stuff["value"];
}
回答by Matthew
foreach ($data as $key => $value){
echo "$key => $value\n";
}
回答by Tim Cooper
foreach($data as $key => $value)
{
// $key and $value variable are available here
// First iteration values would be: $key = "some" and $value = "163"
}