M
M
Mark Kalerin2016-12-04 11:50:02
PHP
Mark Kalerin, 2016-12-04 11:50:02

How to search by keys of a multidimensional array?

There is an array of unlimited nesting level, for example:

$out_data = array(
    'home' => ['city' => 'Miami', 'state' => 'FL', 'location' => ['lat' => '40.00', 'long' => '50.00']],
    'work' => ['main' => ['role' => 'cheif', 'address' => 'Miami, FL'], 'hobby' => ['role' => 'painter', 'address' => null]],
    'age' => 68
);

1. You need to do a search on it so that when you query "age" the value 68 is returned, and when you query "home\location" an array with values ​​is returned [ lat => 40.00, long => 50.00 ].
2. How to add elements to such an array so that when you add an element 'work\new\' to the array, you can add an array of the form '['role' => 'new_work_role', 'address' => 'homeland' ] to it '?
Let me clarify that it is necessary to make a function in which, when passing one parameter of the form 'age' or 'home\location', a search was performed, and when two parameters were added: "home\location" and an array, the addition was carried out. In this case, the depth of the array is unknown, i.e. it may be necessary to add data to the array at "home\location\secret\new1" and even deeper.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pashted, 2016-12-04
@Vosined_12

echo $out_data['age'];

print_r($out_data['home']['location']);

$out_data['work']['new'] = ['role' => 'new_work_role', 'address' => 'homeland'];
print_r($out_data['work']['new']);

searching for a value across multiple keys
$out_data = array(
    'home' => ['city' => 'Miami', 'state' => 'FL', 'location' => ['lat' => '40.00', 'long' => '50.00']],
    'work' => ['main' => ['role' => 'cheif', 'address' => 'Miami, FL'], 'hobby' => ['role' => 'painter', 'address' => null]],
    'age' => 68
);

function get_data($data, $param) {
        $param = explode('\\', $param);

        $result = $data[$param[0]];

        for ($i = 1; $i < count($param); $i++) {
            $result = $result[$param[$i]];
        }

        return $result;
}

print_r(get_data($out_data, 'age'));
print_r(get_data($out_data, 'home\location'));
print_r(get_data($out_data, 'work\main\address'));

adding a new element
function set_data(&$arr, $data, $param) {
        $param = explode('\\', $param);
        $result = &$arr[$param[0]];

        for ($i = 1; $i < count($param); $i++) {
            $result = &$result[$param[$i]];
        }
        $result = $data;
    }

set_data($out_data, ['role' => 'boss', 'address' => 'Secret Base 1'], 'work\main\secret_job\project_1\state\city\street');

result of adding
print_r($out_data);
Array
(
    [home] => Array
        (
            [city] => Miami
            [state] => FL
            [location] => Array
                (
                    [lat] => 40.00
                    [long] => 50.00
                )

        )

    [work] => Array
        (
            [main] => Array
                (
                    [role] => cheif
                    [address] => Miami, FL
                    [secret_job] => Array
                        (
                            [project_1] => Array
                                (
                                    [state] => Array
                                        (
                                            [city] => Array
                                                (
                                                    [street] => Array
                                                        (
                                                            [role] => boss
                                                            [address] => Secret Base 1
                                                        )

                                                )

                                        )

                                )

                        )

                )

            [hobby] => Array
                (
                    [role] => painter
                    [address] => 
                )

        )

    [age] => 68
)

S
Snewer, 2016-12-04
@Snewer

there is no way to check and debug. but I suggest the following logic:

function set_array_value(&$array, $key, $value){
        if(!is_array($key)){
            $key = explode('\\', $key);
        }

        $currentKey = array_shift($key);
        
        if(!is_array($array[$currentKey])){
            if(!isset($array[$currentKey])){
                $array[$currentKey] = [];
            } else {
                $array[$currentKey] = [ $array[$currentKey]];
            }
        }

        if(count($key) > 0){
            set_array_value($array[$currentKey], $key, $value);
        } else {
            if(is_array($array[$currentKey])){
                $array[$currentKey][] =  $value;
            } else {
                $array[$currentKey] = [$array[$currentKey], $value];
            }
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question