php - Search for matching subarray in parent or child of a multidimensional array

693

I have the following multidimensional array to build a dynamic menu:

[
    "3gnitjUdm6" => [
        "name" => "Overview",
        "slug" => "overview",
        "priority" => 1,
        "pages" => [
            "i3OQlLqgqO" => [
                "name" => "Dashboard",
                "url" => "",
                "priority" => 2,
                "subpages" => [],
            ],
            "izma1tvjGd" => [
                "name" => "Settings",
                "url" => "/settings",
                "priority" => 4,
                "subpages" => [],
            ]
        ]
    ],
    "IcSujiIx9A" => [
        "name" => "Content",
        "slug" => "content",
        "priority" => 5,
        "pages" => [
            "3KJdhtCRuI" => [
                "name" => "Users",
                "url" => "/users",
                "priority" => 2,
                "subpages" => [],
            ],
            "M3zw9hq6rW" => [
                "name" => "Pets",
                "url" => "/pets",
                "priority" => 4,
                "subpages" => [],
            ],
        ],
    ],
]

Each section contains an array of pages, and each page can contain an array of subpages. I need to be able to search through this array to find the key of the section using a key and value pair.

private function _find_section($key, $value) {
    foreach($this->menu as $section_key => $section) {
        if(is_array($section[$key])) {
            foreach($section[$key] as $sub_key => $sub) {
                if($sub_key === $value) {
                    return $section_key;
                }
            }
        } elseif(is_string($section[$key])) {
            if($section[$key] === $value) {
                return $section_key;
            }
        } else {
            return false;
        }
    }
}

Running the following code:

_find_section('name', 'Content')

Always returns false.

525

Answer

Solution:

function flatten(array $collection, array $nested_keys = []) {
    $output = [];
    foreach ($collection as $key => $value) {
        foreach ($nested_keys as $nested_key) {
            if (isset($value[$nested_key]) && is_array($value[$nested_key])) {
                $output = array_merge($output, flatten($value[$nested_key], [$nested_key]));
            }
        }
        $output[$key] = $value;
    }

    return $output;
}

function column(array $collection, string $key) {
    return array_combine(
        array_keys($collection),
        array_map(function ($row) use ($key) { return $row[$key]; }, $collection)
    );
}

function find_section(array $menu, string $key, string $value) {
    $set = column(flatten($menu, ['pages', 'subpages']), $key);

    return array_search($value, $set);
}

var_dump(find_section($menu, 'name', 'Dashboard')); // string(10) "i3OQlLqgqO"
203

Answer

Solution:

You might want to try doing a recursive function instead and forego hardcodedforeach() loops, then you can easily search through many levels of your array using this method:

function recurseFind($array,$findK,$findV,$last=false)
    {
        if(is_object($array))
            $array  =   (array) $array;

        foreach($array as $key => $value) {
            if($key == $findK && $value == $findV)
                return $last;

            if(is_array($value))
                $doFind =   recurseFind($value,$findK,$findV,$key);

            if(!empty($doFind))
                return $doFind;
        }
    }

print_r(recurseFind($arr,'name','Dashboard'));

Gives you:

i3OQlLqgqO

People are also looking for solutions to the problem: PHP Wrench (websocket) - problems with cellular connection?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.