PHP:从数组获取密钥? [英] PHP: Get key from array?

查看:80
本文介绍了PHP:从数组获取密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信这是PHP中的超级简单内置函数,但是我还没有看到它.

I am sure that this is super easy and built-in function in PHP, but I have yet not seen it.

这是我目前正在做的事情:

Here's what I am doing for the moment:

foreach($array as $key => $value) {
    echo $key; // Would output "subkey" in the example array
    print_r($value);
}

我可以改为执行以下操作,从而避免在每个foreach循环中编写"$ key => $ value"吗? (伪代码)

Could I do something like the following instead and thereby save myself from writing "$key => $value" in every foreach loop? (psuedocode)

foreach($array as $subarray) {
    echo arrayKey($subarray); // Will output the same as "echo $key" in the former example ("subkey"
    print_r($value);
}

谢谢!

数组:

Array
(
    [subKey] => Array
        (
            [value] => myvalue
        )

)

推荐答案

您可以使用键():

<?php
$array = array(
    "one" => 1,
    "two" => 2,
    "three" => 3,
    "four" => 4
);

while($element = current($array)) {
    echo key($array)."\n";
    next($array);
}
?>

这篇关于PHP:从数组获取密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆