返回key包含此字符串的值
16lz
2021-01-22
I have an array like this:
我有一个像这样的数组:
array(
[root_1] => array(
[level_1] => array(
[id_1] => array(
[foo] = bar
)
)
)
)
Where I know the names (i.e root, level and id) but I do not know the number part of the key.
我知道名称(即根,级别和ID),但我不知道密钥的数字部分。
I need a function that can return any parts of the array where the key contains a given name.
我需要一个函数,可以返回键包含给定名称的数组的任何部分。
Cheers
4 个解决方案
#1
3
Here you go
干得好
function search_incomplete ($needle, $haystack)
{
foreach (new RecursiveIteratorIterator (new RecursiveArrayIterator ($haystack), RecursiveIteratorIterator::SELF_FIRST) as $key => $value)
{
if (strpos ($key, $needle) === 0)
{
return $value;
}
}
}
$array = array (
'root_1' => array (
'level_1' => array (
'foo' => 'bar',
),
),
);
var_dump (search_incomplete ('level', $array));
var_dump (search_incomplete ('root', $array));
var_dump (search_incomplete ('foo', $array));
更多相关文章
- 学习成绩统计-【数组操作】
- 如何在发送之前更改SOAP请求中的名称空间前缀(皇家邮件 - 发送请
- 如何为$ wpdb-> insert创建一个包含column => value对的数组
- 如何在php中为json数组创建搜索功能
- json_encode在数组上返回null
- PHP数组值进入第二个数组[重复]
- 如何在php数组中插入新的键值对?
- mysql_fetch_array返回一个数组,数字为“1”
- PHP-从多维数组中删除重复值