3v4l.org

run code in 300+ PHP versions simultaneously
<?php $query_input = [ '_or'=>[[ ['cats'=>1], ['dogs'=>1] ]] ]; $results = [ ['id'=>0,'cats'=>0,'dogs'=>0], ['id'=>1,'cats'=>0,'dogs'=>0,'lizards'=>3], ['id'=>2,'cats'=>0,'dogs'=>1], ['id'=>3,'cats'=>1,'dogs'=>0], ['id'=>4,'cats'=>1,'dogs'=>1] ]; // hackkk both the filter and find methods need significant cleanup if (isset($query_input['_or'])) { foreach ($query_input['_or'] as $key=>$value) { $query_where[] = [$key, '_OR', null]; } } foreach ($results as $result) { foreach ($query_where as $where) { list($key, $comp, $value) = $where; if ($comp !== '_OR' && !isset($result[$key])) { $skip = ['!isset', $key]; continue; } // the data is not in the result, skip check switch ($comp) { case '_OR': $or = false; foreach ($query_input['_or'][$key] as $or_item) { foreach ($or_item as $comp_key=>$comp_value) { if ($result[$comp_key] != $comp_value) { continue(2); } } $or = true; break; } if (!$or) continue(3); break; case '=': if ($result[$key] != $value) { $skip[] = [$result[$key],$value,'=',$key]; continue(3); } break; case 'LIKE': $pos = strpos($value,'%'); if ($pos===false) $pos = strlen($value); if (substr($result[$key],0,$pos) !== substr($value,0,$pos)) { $skip[] = [$result[$key],$value,'LIKE',$key]; continue(3); } break; } } $out[] = $result; if (sizeof($out) === $query_limit) { break; } } var_dump($out);
Output for 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
Warning: Undefined variable $query_limit in /in/ZBeT5 on line 64 Warning: Undefined variable $query_limit in /in/ZBeT5 on line 64 Warning: Undefined variable $query_limit in /in/ZBeT5 on line 64 array(3) { [0]=> array(3) { ["id"]=> int(2) ["cats"]=> int(0) ["dogs"]=> int(1) } [1]=> array(3) { ["id"]=> int(3) ["cats"]=> int(1) ["dogs"]=> int(0) } [2]=> array(3) { ["id"]=> int(4) ["cats"]=> int(1) ["dogs"]=> int(1) } }
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 Warning: Undefined variable $query_limit in /in/ZBeT5 on line 64 Warning: Undefined variable $query_limit in /in/ZBeT5 on line 64 Warning: Undefined variable $query_limit in /in/ZBeT5 on line 64 array(3) { [0]=> array(3) { ["id"]=> int(2) ["cats"]=> int(0) ["dogs"]=> int(1) } [1]=> array(3) { ["id"]=> int(3) ["cats"]=> int(1) ["dogs"]=> int(0) } [2]=> array(3) { ["id"]=> int(4) ["cats"]=> int(1) ["dogs"]=> int(1) } }
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
Notice: Undefined variable: query_limit in /in/ZBeT5 on line 64 Notice: Undefined variable: query_limit in /in/ZBeT5 on line 64 Notice: Undefined variable: query_limit in /in/ZBeT5 on line 64 array(3) { [0]=> array(3) { ["id"]=> int(2) ["cats"]=> int(0) ["dogs"]=> int(1) } [1]=> array(3) { ["id"]=> int(3) ["cats"]=> int(1) ["dogs"]=> int(0) } [2]=> array(3) { ["id"]=> int(4) ["cats"]=> int(1) ["dogs"]=> int(1) } }
Output for 7.3.32 - 7.3.33
array(3) { [0]=> array(3) { ["id"]=> int(2) ["cats"]=> int(0) ["dogs"]=> int(1) } [1]=> array(3) { ["id"]=> int(3) ["cats"]=> int(1) ["dogs"]=> int(0) } [2]=> array(3) { ["id"]=> int(4) ["cats"]=> int(1) ["dogs"]=> int(1) } }

preferences:
221.4 ms | 402 KiB | 288 Q