3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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]; } } var_dump($query_where); $out = []; $skip = []; 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': foreach ($query_input['_or'][$key] as $or_item) { foreach ($or_item as $comp_key=>$comp_value) { if ($result[$comp_key] != $comp_value) { echo 1; continue(2); } } echo 2; var_dump($result,'okay'); break; // as long as the script hits here, the result will be kep } 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; } var_dump($out);
based on VZCQL
Output for 5.6.0 - 5.6.30, hhvm-3.15.4, 7.0.0 - 7.2.0
array(1) { [0]=> array(3) { [0]=> int(0) [1]=> string(3) "_OR" [2]=> NULL } } 111112array(3) { ["id"]=> int(2) ["cats"]=> int(0) ["dogs"]=> int(1) } string(4) "okay" 2array(3) { ["id"]=> int(3) ["cats"]=> int(1) ["dogs"]=> int(0) } string(4) "okay" 2array(3) { ["id"]=> int(4) ["cats"]=> int(1) ["dogs"]=> int(1) } string(4) "okay" array(5) { [0]=> array(3) { ["id"]=> int(0) ["cats"]=> int(0) ["dogs"]=> int(0) } [1]=> array(4) { ["id"]=> int(1) ["cats"]=> int(0) ["dogs"]=> int(0) ["lizards"]=> int(3) } [2]=> array(3) { ["id"]=> int(2) ["cats"]=> int(0) ["dogs"]=> int(1) } [3]=> array(3) { ["id"]=> int(3) ["cats"]=> int(1) ["dogs"]=> int(0) } [4]=> array(3) { ["id"]=> int(4) ["cats"]=> int(1) ["dogs"]=> int(1) } }