查找数组子键中是否存在键? [英] Find a key exists in the sub keys of an array?

查看:109
本文介绍了查找数组子键中是否存在键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查数组的子键中是否存在键?如果找到了该项目的键,则返回该项目?

How can I check if a key exists in the sub keys of an array? And if that key of the item is found then return that item?

例如,我有这个数组,

Array
(
    [0] => Array
        (
            [a] => Array
                (
                    [quantity_request] => 1
                    [time_created] => 1339688613
                    [variant] => Array
                        (
                        )

                )

        )

    [1] => Array
        (
            [b] => Array
                (
                    [quantity_request] => 1
                    [time_created] => 1339688631
                    [variant] => Array
                        (
                        )

                )

        )

    [2] => Array
        (
            [c] => Array
                (
                    [quantity_request] => 1
                    [time_created] => 1339688959
                    [variant] => Array
                        (
                        )

                )

        )

)

我想找到键'b'并返回它下面的所有内容,就像我要的那样,

I want to find key 'b' and return everything under it, like this is what I am after,

[b] => Array
                (
                    [quantity_request] => 1
                    [time_created] => 1339688631
                    [variant] => Array
                        (
                        )

                )

我尝试了一下,但没有任何回报,

I try with this, but nothing returns,

if (array_key_exists('b', $this->content)) {
                echo "The 'b' element is in the array";

}

有什么想法吗?

推荐答案

function get_letter($letter){
    foreach($this->content as $v){
        if(array_key_exists($letter, $v) {
            return $v[$letter];
        }
    }
    return false;
}

$array = get_letter('a');

这篇关于查找数组子键中是否存在键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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