【问题标题】:Get the name of an index in an array [duplicate]获取数组中索引的名称[重复]
【发布时间】:2012-11-27 16:50:54
【问题描述】:

可能重复:
Get first key in a [possibly] associative array?

我有这个数组:

Array
(
    ['foobar'] => Array
        (
            [key1] => value1
            [key2] => value2
        )

)

我想获得第一个索引的名称 (foobar)。我该怎么做?

【问题讨论】:

    标签: php


    【解决方案1】:

    另一种方法是,

    $keys = array_keys($array);
    
    echo $keys[0];
    

    【讨论】:

    • 也许可以缩短它 $firstKey = array_keys($array)[0]; ?
    【解决方案2】:

    假设您没有使用过each()next()prev() 或任何其他方式,您是否推进了数组指针:

    key($array);
    

    【讨论】:

      猜你喜欢
      • 2013-10-05
      • 2021-01-22
      • 2017-09-19
      • 1970-01-01
      • 2015-06-04
      • 1970-01-01
      • 1970-01-01
      • 2016-03-20
      • 1970-01-01
      相关资源
      最近更新 更多