douneiben2240 2013-03-05 01:48
浏览 43
已采纳

PHP - 获取嵌套数组的KEY

I have an array called $plugins that looks something like this :

Array
(
    [path/to/file.php] => Array
        (
            [Name] => somevalue_a
            [TextDomain] => somevalue_b
            [value_c] => somevalue_c
            [value_d] => somevalue_d
            ...
            ...
            ..

        )
    [path/to/file2.php] => Array
        (
            [Name] => somevalue_a
            [TextDomain] => somevalue_b
            [value_c] => somevalue_c
            [value_d] => somevalue_d
            ...
            ...
            ..
        )
)

Now, I am having trouble to get the KEY name (which is path)of each array element ..

function get_plugin_data(){

    foreach ($plugins as $plugin => $data) {
    $plugin_data = $plugins[$plugin];

    // Start simple DEBUG
    echo '</br>===============================</br>' ;
    echo '</br><b>Plugin Name : </b>'. $data[Name]; .'</br>' ;
    echo '</br><b>Plugin Path : </b>'. key($plugins)   .'</br>' ; // <-- Problem here
    echo '</br>TextDomain set  : '. $data[TextDomain] .'</br>' ;
    echo '</br>===============================</br>' ;
    // End DEBUG
    }
}

When using key($plugins) it gives me always the same value (first one). When using key($data) it is giving me the FIRST LETTER only.. (??)

How can I get the this key of each nested array ?

  • 写回答

3条回答 默认 最新

  • dpxnrx11199 2013-03-05 01:55
    关注

    just return $plugin, not key($plugin). $plugin should already be the key.

    to elaborate, when you use the syntax:

    foreach ($plugins as $plugin => $data)

    it is setting $plugin to the key, and $data to it's value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?