3v4l.org

run code in 300+ PHP versions simultaneously
<?php function item_by_value($array,$key,$value) { // returns FIRST matching element only, presumes ID if ((!is_array($array)) || empty($key) || empty($value)) return false; for ($i=0; $i < count($array); $i++) { $item = (array) $array[$i]; if ($item[$key] == $value) { return $array[$i]; break; } } return null; } function article_link($article,$articles) { if (is_null($article) || !is_object($article)) {return '';} if ($article->parentPage == 'self') { return '/'.$article->id; } else { $parent = item_by_value($articles,'id',$article->parentPage); return is_null($parent) ? 'nullo' : article_link($parent->id,$articles).'/'.$article->id; } } $articles = json_decode('{"articles":[{"id":"about","title":"Significant Object","bodyHtml":"about-main.html","parentPage":"self","banner":"stock"},{"id":"workshops","title":"Workshops","bodyHtml":"workshops.html","parentPage":"self","banner":null},{"id":"making","title":"Making","bodyHtml":"making.html","parentPage":"self"},{"id":"the-lion-the-witch-and","title":"The Lion, The Witch and The Wardrobe","bodyHtml":"the-lion-the-witch.html","parentPage":"past"},{"id":"circus-1903","title":"Circus 1903","bodyHtml":"circus-1903.html","parentPage":"current","banner":"stock"},{"id":"dissonance","title":"Dissonance","bodyHtml":"dissonance.html","parentPage":"current"},{"id":"handspring-uk","title":"Handspring UK","bodyHtml":"handspring-uk.html","parentPage":"past","banner":"stock"},{"id":"james-ii","title":"James II","bodyHtml":"james-ii.html","parentPage":"past"},{"id":"die-bruder-lowenherz","title":"Die Brüder Löwenherz (The Brothers Lionheart)","bodyHtml":"lowenherz.html","parentPage":"past"},{"id":"newman","title":"Newman","bodyHtml":"newman.html","parentPage":"past"},{"id":"stiller","title":"Stiller","bodyHtml":"stiller.html","parentPage":"past"},{"id":"the-loris","title":"The Loris","bodyHtml":"the-loris.html","parentPage":"past"},{"id":"reviews-of-stiller","title":"Press Reviews of Stiller","bodyHtml":"reviews-of-stiller.html","parentPage":"stiller","banner":"none"},{"id":"jubilee-salute","title":"Jubilee Salute","bodyHtml":"jubilee-salute-handspring-UK.html","parentPage":"past"},{"id":"crow","title":"Crow","bodyHtml":"crow.html","parentPage":"past"}]}'); $test = $articles->articles[10]; $array = $articles->articles; echo article_link($test,$array);
Output for 5.6.0 - 5.6.27, 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
nullo
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 nullo

preferences:
149.99 ms | 401 KiB | 202 Q