Answer the question
In order to leave comments, you need to log in
How to find nesting in multidimensional array and add elements?
Hello everyone!) I'm a little stuck on the task, I ask for help)
There is an array, for example
"it_array": [
"select": ["val1", "val2", "val3"],
"relations": [
"relation_1_title": [
"select": ["some_val"],
"relations": [
"relation_2_title": [
"select": ["some_val1", "some_val2"],
"relations": [
"relation_3_title": []
]
]
]
]
]
]
Answer the question
In order to leave comments, you need to log in
Found on google:
Recursive variant:
function getArray($array, $index) {
if (!is_array($array)) return null;
if (isset($array[$index])) return $array[$index];
foreach ($array as $item) {
$return = getArray($item, $index);
if (!is_null($return)) {
return $return;
}
}
return null;
}
function getArray($array, $index) {
$queue = array($array);
while (($item = array_shift($queue)) !== null) {
if (!is_array($item)) continue;
if (isset($item[$index])) return $item[$index];
$queue = array_merge($queue, $item);
}
return null;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question