PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php function getIn($data, $arrsearch) { $current = $data; $result = []; foreach ($arrsearch as $key) { if (is_array($current) && array_key_exists($key, $current)) { $current = $current[$key]; $result[] = $current; // Добавляем текущее значение в массив результатов echo "TEST"; var_dump( $result); } 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']));
Show:  
Copy Clear