php - json_encode with a different result after array_filter -
i want json_encode return this
[{key: "value"},{key:"value"},...]
instead this:
{"1": {key: "value"}, "2": {key: "value"}, ...}
the result fine until did array_filter
... strange...
function somefunction($id, $ignore = array()) { $ignorefunc = function($obj) use ($ignore) { return !in_array($obj['key'], $ignore); }; global $db; $q = "some query"; $rows = $db->givemesomerows(); $result = array(); if ($rows) { // mapping i've done $result = array_map(array('someclass', 'somemappingfunction'), $rows); if (is_array($ignore) && count($ignore) > 0) { /////// problem after line //////// $result = array_filter($result, $ignorefunc); } } return $result; }
so again, if comment out array_filter
want json_encode
on whatever somefunction
returns, if not json-object.
if var_dump
$result
before , after array_filter
it's same type of php-array, no strings in keys , on.
you want array
getting json
object because array not start 0
trying using array_values
reset array
example
$arr = array(1=>"a",2=>"fish"); print(json_encode($arr)); print(json_encode(array_values($arr)));
output
{"1":"a","2":"fish"} ["a","fish"]
replace
$result = array_filter($result, $ignorefunc);
with
$result = array_filter($result, $ignorefunc); $result = array_values($result);
Comments
Post a Comment