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));

更多相关文章

  1. 学习成绩统计-【数组操作】
  2. 如何在发送之前更改SOAP请求中的名称空间前缀(皇家邮件 - 发送请
  3. 如何为$ wpdb-> insert创建一个包含column => value对的数组
  4. 如何在php中为json数组创建搜索功能
  5. json_encode在数组上返回null
  6. PHP数组值进入第二个数组[重复]
  7. 如何在php数组中插入新的键值对?
  8. mysql_fetch_array返回一个数组,数字为“1”
  9. PHP-从多维数组中删除重复值

随机推荐

  1. 使用jQuery在AJAX调用中的url字段格式
  2. Typeahead 0.10.2没有在Rails 4 / Bootst
  3. 告诉javascript首先运行jquery ajax
  4. GET ajax请求发送到同一个php文件
  5. 无法使用jquery发送简单的ajax请求来获取
  6. 解析PHP的基本文本输出
  7. 利用jQuery实现CheckBox全选/全不选/反选
  8. jquery中的globalEval()源码分析
  9. 从jQuery每个循环中删除item [i]
  10. Jquery 特效 图片轮转 菜单