I'm using this in a project of mine, but I'm using null
as a muted state. (php 5.6)
<?php
function &option_select($options, $default, ...$path) {
if(!isset($path[0])) { return $options; }
$cursor = &$options;
foreach($path as $key) {
if(isset($cursor[$key])) { $cursor = &$cursor[$key]; } else { return $default; }
} return $cursor;
}
It's a function that will lookup a path
in a tree.
echo option_select([A=>[B=>[C=>'hello world']]], 'NO?', 'A', 'B', 'C');
As you can see. I'm looping over the values over a array: path
. In the beginning I'm setting a cursor as reference to a array I have to search in. Every time I find a new
key, I'm setting the reference of the cursor to the current key I'm searching for. If you have xdebug
and a good GUI, you can rewrite it to the algorithm you want. It's the isset($cursor[$key])
you have to change. Or inject it as a function: f($cursor, $key, $default) => (bool)