PHP:打印关联数组
在 PHP 中,我有一个像这样的关联数组
$a = array('who' => 'one', 'are' => 'two', 'you' => 'three');
How to write a foreach
循环来遍历数组并访问数组键和值,以便我可以操作它们(换句话说,我将能够将 who
和 one
分配给两个变量 $key
和 $value
?
In PHP, I have an associative array like this
$a = array('who' => 'one', 'are' => 'two', 'you' => 'three');
How to write a foreach
loop that goes through the array and access the array key and value so that I can manipulate them (in other words, I would be able to get who
and one
assigned to two variables $key
and $value
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@Thiago 已经提到了访问密钥和相应值的方式。这当然是正确且首选的解决方案。
然而,因为你说
我想建议另外两种方法
如果您只想操纵该值,请将其作为参考访问
如果您想同时操作键和值,则应该采用其他方式
@Thiago already mentions the way to access the key and the corresponding value. This is of course the correct and preferred solution.
However, because you say
I want to suggest two other approaches
If you only want to manipulate the value, access it as reference
If you want to manipulate both the key and the value, you should going an other way