PHP:深度数组,如何返回当前键名? [英] PHP: high depth array, how do return current key name?
本文介绍了PHP:深度数组,如何返回当前键名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个来自json_decode结果的巨大数组(关联设置为true),并具有以下代码来检查(其中一个数组是随机序列)是否具有键'set_true'
I have a huge array from a json_decode result (assoc set to true) and have the following code to check if (one of the arrays within, a random serial) has the key 'set_true'
$out = "";
foreach ($array as $sub) {
//$out[] = $sub['set_true'];
if (in_array($sub['set_true'], $sub) && $sub['set_true'] == '1' ) {
$out[] = 'User: ' . $sub . ' has set_true = 1';
}
}
该代码列出了所有具有该阵列键集的用户到1,但是$ sub返回'array'而不是我当前所在的键! (随机序列)
That code lists all the users with that array key set to 1, but $sub returns 'array' and not the current key I'm on! (the random serial)
如何退货?
推荐答案
如果使用 foreach
,想知道您当前在循环中使用的密钥,可以使用以下语法:
If you are looping through an array with foreach
, and want to know the key you're currently on in the loop, you can use this syntax :
foreach ($array as $key => $value) {
// $key contains the name of the current key
// and $value the current value
}
这篇关于PHP:深度数组,如何返回当前键名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文