PHP多维数组:检索给定键的值



我有一个多维数组,像这样:

$sidebar_booking = array(
    'booking' => array(
        'levels' => array('1'),
        'title' => 'Booking',
        'icon' => 'fa-calendar',
        'sub' => array(
            'rates-availability' => array(
                'levels' => array('1'),
                'title' => 'Tariffe e Disponibilità',
                'sub' => array(
                    'booking-overview' => array(
                        'levels' => array('1'),
                        'title' => 'Panoramica',
                        'url' => home('/ctrl/booking/overview/'),
                    ),
                    'booking-setup' => array(
                        'levels' => array('1'),
                        'title' => 'Setup Camere / Tariffe',
                        'url' => home('/ctrl/booking/setup/'),
                    ),
                    'booking-prices' => array(
                        'levels' => array('1'),
                        'title' => 'Modifica Prezzi',
                        'url' => home('/ctrl/booking/prices/'),
                    ),
                    'booking-availability' => array(
                        'levels' => array('1'),
                        'title' => 'Modifica Prezzi',
                        'url' => home('/ctrl/booking/prices/'),
                    ),
                    'booking-openclose' => array(
                        'levels' => array('1'),
                        'title' => 'Apri / Chiudi Camere',
                        'url' => home('/ctrl/booking/openclose/'),
                    ),
                    'booking-restrictions' => array(
                        'levels' => array('1'),
                        'title' => 'Restrizioni',
                        'url' => home('/ctrl/booking/restrictions/'),
                    ),
                    'booking-rates' => array(
                        'levels' => array('1'),
                        'title' => 'Tariffe',
                    ),
                ),
            ),
            'booking-promo' => array(
                'levels' => array('1'),
                'title' => 'Promozioni',
                'url' => home('/ctrl/booking/promo/'),
            ),
            'booking-reservations' => array(
                'levels' => array('1'),
                'title' => 'Prenotazioni',
                'url' => home('/ctrl/booking/reservations/'),
            ),
        )
    ),
);

我可以用递归函数搜索一个特定键的存在性:

function array_key_exists_r($needle, $haystack){
    $result = array_key_exists($needle, $haystack);
    if ($result) return $result;
    foreach ($haystack as $v) {
        if (is_array($v)) {
            $result = array_key_exists_r($needle, $v);
        }
        if ($result) return $result;
    }
    return $result;
};

一切顺利。
现在,一旦找到特定的键,如何检索呢?例如:

retrieve_value_of('booking-setup', $sidebar_booking);

应该返回数组:

array(
    'levels' => array('1'),
    'title' => 'Setup Camere / Tariffe',
    'url' => home('/ctrl/booking/setup/'),
)

Thanks in advance

我更新了你的函数,它给出了预期的输出。请按如下方式尝试。

function array_key_exists_r($needle, $haystack){
    $result = array_key_exists($needle, $haystack);
    if ($result) 
    {
        foreach ($haystack as $a=>$v) 
        {
            if($needle == $a)
                return $haystack[$a];
            if (is_array($v)) {
                $result = array_key_exists_r($needle, $v);
            }
            if ($result) return $result;
        }
    }
    foreach ($haystack as $v) {
        if (is_array($v)) {
            $result = array_key_exists_r($needle, $v);
        }
        if ($result) return $result;
    }
    return $result;
};

相关内容

  • 没有找到相关文章

最新更新