【问题标题】:find numeric key of associative index of array in array [duplicate]在数组中查找数组关联索引的数字键[重复]
【发布时间】:2014-11-12 21:24:05
【问题描述】:

我不明白如何在这种结构中获取“Index1”的数字索引:

$arr = array('Index','Index1' => array("one","two","three"),'Index2');

我正在尝试像这样使用 array_search:

$index = array_search("Index1",$arr);

但它不起作用; 谢谢

【问题讨论】:

  • Index1 没有数字索引 (see for yourself)
  • 索引为Index1。字符串索引没有“数字索引”。
  • 你也可以将数组写成array(0 => 'Index', 'Index1' => .., 1 => 'Index2') 并产生完全相同的结构
  • 确实,它不存在@kingkero

标签: php


【解决方案1】:

如前所述,该元素没有数字索引,因为它的键是字符串。

但是,如果您只想找到它在数组中的位置:

$position = array_search("Index1",array_keys($arr),true);

【讨论】:

  • $position 变为 0 不正确
  • @TraktorFieldwork 糟糕,忘记设置第三个参数。查看工作编辑
  • 谢谢!这正是我需要的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-22
  • 2015-09-24
  • 2012-05-18
  • 1970-01-01
  • 1970-01-01
  • 2021-06-21
相关资源
最近更新 更多