PHP Filter Multidimensional Array By Value

PHP Filter Multidimensional Array By Value

Now you can filter the multidimensional array using php. You will able to find root index data if index matches the value.

Main Array

$data = array(
    0  =>  array(
        'text'       => 'Dashboard',
        'url'        => '/dashboard',
        'submenu'    => [
            [
                'text'       => 'Index',
                'url'        => '/Basic'
            ],
            [
                'text'       => 'Basic',
                'url'        => '/Basic'
            ]
        ]
    ),
    1  =>  array(
        'text'       => 'Catgory',
        'url'        => '/Catgory',
        'submenu'    => [
            [
                'text'       => 'Basic',
                'url'        => '/Catgory/Basic',
                'submenu'    => [
                    [
                        'text'       => 'Child',
                        'url'        => '/Catgory/Basic/Child',
                        'submenu'    => [
                            [
                                'text'       => 'Index',
                                'url'        => '/Catgory/Basic/Child',
                            ],
                            [
                                'text'       => 'Basic',
                                'url'        => '/Catgory/Basic/Child',
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ),
    2  =>  array(
        'text'       => 'Page',
        'url'        => '/dashboard'
    )
);

Now, we need to find value "Index" from this list. So I have create a function to find it.

function find($dataset, $serach, $return_set = array(), $parent_index = -1, $submenu = null)
{
    global $dataset_main;
    foreach ($dataset as $index => $data) {
        if (strtolower($data['text']) == strtolower($serach)) {
            if ($submenu != null) {
                $return_set[] = $dataset_main[$parent_index];
            } else {
                $return_set[] = $dataset[$index];
            }

        } elseif (array_key_exists('submenu', $data)) {
            if ($submenu == null) {
                $parent_index = $index;
            }

            $return_set = find($data['submenu'], $serach, $return_set, $parent_index, 'submenu');
        }
    }
    return $return_set;
}

Final Code

<?php

$data = array(
    0 => array(
        'text' => 'Dashboard',
        'url' => '/dashboard',
        'submenu' => [
            [
                'text' => 'Index',
                'url' => '/Basic',
            ],
            [
                'text' => 'Basic',
                'url' => '/Basic',
            ],
        ],
    ),
    1 => array(
        'text' => 'Catgory',
        'url' => '/Catgory',
        'submenu' => [
            [
                'text' => 'Basic',
                'url' => '/Catgory/Basic',
                'submenu' => [
                    [
                        'text' => 'Child',
                        'url' => '/Catgory/Basic/Child',
                        'submenu' => [
                            [
                                'text' => 'Index',
                                'url' => '/Catgory/Basic/Child',
                            ],
                            [
                                'text' => 'Basic',
                                'url' => '/Catgory/Basic/Child',
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ),
    2 => array(
        'text' => 'Page',
        'url' => '/dashboard',
    ),
);

echo "<pre>";
print_r($data);
$dataset_main = $data;

function find($dataset, $serach, $return_set = array(), $parent_index = -1, $submenu = null)
{
    global $dataset_main;
    foreach ($dataset as $index => $data) {
        if (strtolower($data['text']) == strtolower($serach)) {
            if ($submenu != null) {
                $return_set[] = $dataset_main[$parent_index];
            } else {
                $return_set[] = $dataset[$index];
            }

        } elseif (array_key_exists('submenu', $data)) {
            if ($submenu == null) {
                $parent_index = $index;
            }

            $return_set = find($data['submenu'], $serach, $return_set, $parent_index, 'submenu');
        }
    }
    return $return_set;
}

echo "<br><br>Result:<br><br>";

$result = find($data, 'Index');
print_r($result);

Output

Array
(
    [0] => Array
        (
            [text] => Dashboard
            [url] => /dashboard
            [submenu] => Array
                (
                    [0] => Array
                        (
                            [text] => Index
                            [url] => /Basic
                        )

                    [1] => Array
                        (
                            [text] => Basic
                            [url] => /Basic
                        )

                )

        )

    [1] => Array
        (
            [text] => Catgory
            [url] => /Catgory
            [submenu] => Array
                (
                    [0] => Array
                        (
                            [text] => Basic
                            [url] => /Catgory/Basic
                            [submenu] => Array
                                (
                                    [0] => Array
                                        (
                                            [text] => Child
                                            [url] => /Catgory/Basic/Child
                                            [submenu] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [text] => Index
                                                            [url] => /Catgory/Basic/Child
                                                        )

                                                    [1] => Array
                                                        (
                                                            [text] => Basic
                                                            [url] => /Catgory/Basic/Child
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [2] => Array
        (
            [text] => Page
            [url] => /dashboard
        )

)


Result:

Array
(
    [0] => Array
        (
            [text] => Dashboard
            [url] => /dashboard
            [submenu] => Array
                (
                    [0] => Array
                        (
                            [text] => Index
                            [url] => /Basic
                        )

                    [1] => Array
                        (
                            [text] => Basic
                            [url] => /Basic
                        )

                )

        )

    [1] => Array
        (
            [text] => Catgory
            [url] => /Catgory
            [submenu] => Array
                (
                    [0] => Array
                        (
                            [text] => Basic
                            [url] => /Catgory/Basic
                            [submenu] => Array
                                (
                                    [0] => Array
                                        (
                                            [text] => Child
                                            [url] => /Catgory/Basic/Child
                                            [submenu] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [text] => Index
                                                            [url] => /Catgory/Basic/Child
                                                        )

                                                    [1] => Array
                                                        (
                                                            [text] => Basic
                                                            [url] => /Catgory/Basic/Child
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)
Related posts
Write a comment
Loading...