Recurring arrays in php

Question:

Can you please tell me how recursion works, and how to write such a function so that it can go through the entire array and remove a specific element. Or just give, please, an example of such a function. I'll do the rest myself, I can't figure out how this should work. Thank you.

(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => text
                    [1] => created_ad
                )

            [1] => required
        )
    [1] => Array
        (
            [0] => Array
                (
                    [0] => id_user
                    [1] => priority
                    [2] => status
                    [3] => created_ad
                )

            [1] => integer
        )

    [2] => Array
        (
            [0] => Array
                (
                    [0] => text
                    [1] => img
                )

            [1] => string
        )

    [3] => Array
        (
            [0] => id_user
            [1] => safe
        )

    [4] => Array
        (
            [0] => priority
            [1] => default
            [value] => 0
        )

    [5] => Array
        (
            [0] => status
            [1] => default
            [value] => 0
        )

    [6] => Array
        (
            [0] => priority
            [1] => in
            [range] => Array
                (
                    [0] => 0
                    [1] => 1
                    [2] => 2
                )

        )

    [7] => Array
        (
            [0] => status
            [1] => in
            [range] => Array
                (
                    [0] => 0
                    [1] => 1
                    [2] => 2
                )

        )

)

Remove id_user and created_ad from this array

private function unsetRules($rules = null)
{
    foreach ($rules as $key => &$rule) {

        if (is_array($rules[$key])) {
            $this->unsetRules($rules[$key]);
        }

        if ($rules[$key] === 'id_user') {
            unset($rules[$key]);

        }
    }

    return $rules;
}

This is how nothing comes out = (

Answer:

private function unsetRules(&$array)
{
    foreach ($array as $key => &$value) {
        if (is_array($value)) {
            $this->unsetRules($value);
        } else {
            if (in_array($array[$key], $this->deleteRules())) {
                unset($array[$key]);
            }
        }
    }
}

Everything turns out quite simply, you had to use the array by reference

Scroll to Top