3v4l.org

run code in 300+ PHP versions simultaneously
<?php $resultset = [ // query just once, collect the full tree ["ID" => 1, "Name" => "News", "Father" => 0], ["ID" => 2, "Name" => "Articles", "Father" => 0], ["ID" => 3, "Name" => "Politics", "Father" => 1], ["ID" => 4, "Name" => "Politics", "Father" => 2], ["ID" => 5, "Name" => "World", "Father" => 3], ["ID" => 6, "Name" => "World", "Father" => 4] ]; $path = "/News/Politics/World"; // $path = "/Articles/The Funnies/Garfield"; // a test case that fails // News: parent = 0, id = 1 // \ // Politics: parent = 1, id = 3 // > the intended logic // World: parent = 3, id = 5 // / $cfg['categories_separator'] = "/"; $breadcrumbs = explode($cfg['categories_separator'], trim($path, "/")); // var_export($breadcrumbs); // see what is generated $parent = 0; // default value foreach ($breadcrumbs as $crumb) { $id = false; // set invalid value for success check foreach ($resultset as $row) { if ($row["Name"] == $crumb && $parent == $row["Father"]) { // qualifying match $id = $parent = $row["ID"]; // dual declaration break; // break inner loop, progress to next $crumb } } if (!$id) { // inner loop failed to find qualifying match echo "Uh-oh, Broken Breadcrumb Path -- $crumb not found in $path\n"; break; // break outer loop, path is invalid } // echo "ID = $id for $crumb\n"; // uncomment to see progress } echo "ID = $id for $crumb\n"; // echo the result
Output for 5.6.38, 7.1.0 - 7.1.22, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
ID = 5 for World
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 ID = 5 for World

preferences:
174.28 ms | 402 KiB | 171 Q