通过索引访问关联数组项
我想通过索引访问关联数组中的信息
$arr = Array(
['mgm19'] => Array(
['override'] => 1
)
);
$override1 = $arr['mgm19']['override'];
$override2 = $arr[0]['override'];
,但我从 override2 中什么也没得到,为什么?
I want to access information in an associative array by index like so
$arr = Array(
['mgm19'] => Array(
['override'] => 1
)
);
$override1 = $arr['mgm19']['override'];
$override2 = $arr[0]['override'];
but i get nothing from override2 why ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
因为
$arr
只有一个索引,mgm19
。没有任何内容与索引0
关联。如果您不知道索引或不想使用它,请使用foreach
:Because
$arr
has only one index,mgm19
. Nothing is associated to the index0
. If you don't know the index or don't want to use it, useforeach
:不,它是说您可以同时使用数字和字符串索引,而不是您可以使用其中之一来访问它们。请记住,键是唯一值标识符,如果允许使用数字或字符串,则无法使用它们在数组中的数字位置来访问它们,请采用以下数组:
我们允许使用混合数据类型作为键,而您无法以
[0]
方式访问[mgm19]
的原因是这不是它的密钥。我希望这是有道理的:P
No, it's saying you can use both numeric and string indicies, not that you can access them using one or the other. Remember a key is a unique value identifier, and if you're allowed to use a number or a string you cannot access them using their numeric position in the array, take the following array:
We're allowed to have mixed datatypes as a key, and the reason you cannot access
[mgm19]
as[0]
is because that's not its key.I hope that made sense :P
我想看看这个函数, http://www.php .net/manual/en/function.array-values.php 看起来可能会有帮助:)
I would have a look at this function, http://www.php.net/manual/en/function.array-values.php which looks like it could be helpfull :)
无法使用数组中的数字位置来访问关联数组。
从技术上讲,PHP 中的所有数组都是相同的。数组中的每个位置都用数值或字符串定义,但不能同时定义两者。
如果您想检索数组中的特定元素,但不使用您定义的关联索引,请使用 当前,上一个 , 下一个, 重置,以及 结束 函数。
Associative arrays cannot be accessed using the numerical position in the array.
Technically, all arrays in PHP are the same. Each position in an array is defined with either a numerical value or a string, but not both.
If you want to retrieve a specific element in the array, but not use the associative index you have defined, then use the current, prev, next, reset, and end functions.