3v4l.org

run code in 300+ PHP versions simultaneously
<?php function createTree($array){ $tree = []; foreach($array as $v){ $v = str_split($v); $tmp = &$tree; while($c = array_shift($v)) $tmp = &$tmp[strtolower($c)]; unset($tmp); } return $tree; } function findNode($value, $treeTmp){ foreach($value as $k){ if(!array_key_exists(strtolower($k), $treeTmp)) return FALSE; else $treeTmp = $treeTmp[$k]; } return TRUE; } /* ------------ */ $data = ["round", "roundish", "word"]; $searchValue = "round"; //Normal lookU **very slow** $result = in_array($searchValue, $data); var_dump($result); //Faster lookUp $tree = createTree($data); print_r($tree); $result = findNode(str_split($searchValue), $tree); var_dump($result); //PROBLEM -> It also finds values which don't exist in '$data' $searchValue = "ro"; //I DO NOT EXIST IN $data $result = findNode(str_split($searchValue), $tree); var_dump($result); ?>
Output for git.master, git.master_jit, rfc.property-hooks
bool(true) Array ( [r] => Array ( [o] => Array ( [u] => Array ( [n] => Array ( [d] => Array ( [i] => Array ( [s] => Array ( [h] => ) ) ) ) ) ) ) [w] => Array ( [o] => Array ( [r] => Array ( [d] => ) ) ) ) bool(true) bool(true)

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
144.18 ms | 409 KiB | 5 Q