i've javascript file passes argument backend php file. try find out values true.
this passed parameter,
{"2":true,"3":false,"4":true} this php code extract keys of true arrays,
<?php print_r(loop_filters($_request['filters'])); // functions starts here function loop_filters($filters) { $filters = json_decode($filters); // return $filters; $filter_array = []; while($filter_key = current($filters)) { if($filter_key === true) { $filter_array[] = key($filters); } next($filters); } return $filter_array; } ?> however output
array ( [0] => 2 ) why not detecting 4 true , give output like
array ( [0] => 2 , [1] => 4) what's wrong , how can fix it? thanks
your code doesn't work because of this:
while($filter_key = current($filters)) because in second iteration value be: false , know:
while(false) this won't run, end loop after first iteration already.
how solve it? remove next() calls , replace while loop foreach loop, e.g.
function loop_filters($filters) { $filters = json_decode($filters); foreach($filters $key => $filter_key) { if($filter_key === true) { $filter_array[] = $key; } } return $filter_array; }
Comments
Post a Comment