0

I have a multidimensional array like this:

$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/'),
            ),
        )
    ),
);

I can search the existence of a specific key with a recursive function:

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

So far so good.
Now, how can I retrieve the value once I found a specific key? I.e., a function like:

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

should return the array:

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

Thanks in advance

0

1 Answer 1

1

I updated your function and it is giving expected output. Please give a try as following.

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;
};
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.