时间:2021-07-01 10:21:17 帮助过:4人阅读
是转成这个:{"data":[0,0,0,0,4,0],"result":0}
额,为什么要这么干?是不是用json_decode的时候 出现了object(stdClass)
可以用这个转换
function object_array($array){
if(is_object($array)){
$array = (array)$array;
}
if(is_array($array)){
foreach($array as $key=>$value){
$array[$key] = object_array($value);
}
}
return $array;
}
网上找的
请问大神,这个东西到底是什么呢?object(stdClass),产生原因是什么呢?谢谢!
或者这样说:
从sql返回的结果集是一个字段:count字段,行数有若干行,例子里是6行。想输出成这个结构:{"data":[0,0,0,0,4,0],"result":0}
$a = '{"data":[{"count":"0"},{"count":"0"},{"count":"0"},{"count":"0"},{"count":"4"},{"count":"0"}],"result":0}';$arr = json_decode($a, true);foreach($arr as $k=>$v){ if($k == 'data'){ foreach($v as $vv){ $res[$k][] = $vv['count']; } }else{ $res[$k] = $v; }}echo json_encode($res);
{"data":["0","0","0","0","4","0"],"result":0}
怎么把字符串“0”改成整数0?
{"data":[0,0,0,0,4,0],"result":0}
$s = '{"data":[{"count":"0"},{"count":"0"},{"count":"0"},{"count":"0"},{"count":"4"},{"count":"0"}],"result":0}';$a = json_decode($s, true);$a['data'] = array_map('current', $a['data']);echo json_encode($a);
{"data":["0","0","0","0","4","0"],"result":0}如果你不想数字值加引号就
array_walk_recursive($a, function(&$v) {$v= intval($v);});echo json_encode($a);
{"data":[0,0,0,0,4,0],"result":0}