通过索引访问关联数组项

发布于 2024-09-07 03:17:26 字数 239 浏览 4 评论 0原文

我想通过索引访问关联数组中的信息

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夜还是长夜 2024-09-14 03:17:26

因为$arr只有一个索引,mgm19。没有任何内容与索引 0 关联。如果您不知道索引或不想使用它,请使用 foreach

  foreach($arr as $value) {
     echo $value['override'];
     break; /* breaking so that we only read the first value of the array */
  }

Because $arr has only one index, mgm19. Nothing is associated to the index 0. If you don't know the index or don't want to use it, use foreach:

  foreach($arr as $value) {
     echo $value['override'];
     break; /* breaking so that we only read the first value of the array */
  }
给我一枪 2024-09-14 03:17:26

php.net/manual/en/language.types.array.php “索引数组类型和关联数组类型在 PHP 中是相同的类型,都可以包含整数和字符串索引。”我可能是错的,但这是否意味着它应该已经包含数字索引?

不,它是说您可以同时使用数字和字符串索引,而不是您可以使用其中之一来访问它们。请记住,键是唯一值标识符,如果允许使用数字或字符串,则无法使用它们在数组中的数字位置来访问它们,请采用以下数组:

$arr = Array(
   [mgm19] => Array(
    [override] => 1
   ),
   [0] => Array(
    [override] => 1
   )
);

我们允许使用混合数据类型作为键,而您无法以 [0] 方式访问 [mgm19] 的原因是这不是它的密钥。

我希望这是有道理的:P

php.net/manual/en/language.types.array.php "The indexed and associative array types are the same type in PHP, which can both contain integer and string indices." I may be wrong, but doesn't that mean it should already contain a numeric index?

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:

$arr = Array(
   [mgm19] => Array(
    [override] => 1
   ),
   [0] => Array(
    [override] => 1
   )
);

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

怕倦 2024-09-14 03:17:26
$arr = Array( 
    ['mgm19'] => Array( 
        ['override'] => 1 
    ) 
); 

$override1 = $arr['mgm19']['override']; 
$arrkeys = array_keys($arr);
$override2 = $arr[$arrkeys[0]]['override']; 
$arr = Array( 
    ['mgm19'] => Array( 
        ['override'] => 1 
    ) 
); 

$override1 = $arr['mgm19']['override']; 
$arrkeys = array_keys($arr);
$override2 = $arr[$arrkeys[0]]['override']; 
任性一次 2024-09-14 03:17:26

我想看看这个函数, 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 :)

送你一个梦 2024-09-14 03:17:26

无法使用数组中的数字位置来访问关联数组。

从技术上讲,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文