Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?php
function getIn($data, $arrsearch) {
$current = $data;
$result = [];
foreach ($arrsearch as $key) {
var_dump( $current[$key]);
if (is_array($current) && array_key_exists($key, $current)) {
$current = $current[$key];
$result[] = $current; // Добавляем текущее значение в массив результатов
} else {
return null;
}
}
if (is_array($result) && !array_key_exists(1, $result)) {
$result = $result[0];
}
else {
$result = end($result);
}
return $result;
}
$data = [
'user' => 'ubuntu',
'hosts' => [
['name' => 'web1'],
['name' => 'web2', null => 3]
]
];
var_dump(getIn($data, ['hosts', 1, 'name']));