json转数组

WBOY
Release: 2016-06-20 12:34:45
Original
933 people have browsed it

{"data":[{"count":"0"},{"count":"0"},{"count":"0"},{"count":"0"},{"count":"4"},{"count":"0"}],"result":0}

这个json怎么转为

{"data":[0,0,0,0,40],"result":0}


回复讨论(解决方案)

是转成这个:{"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);
Copy after login

{"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);
Copy after login
{"data":["0","0","0","0","4","0"],"result":0}
Copy after login
如果你不想数字值加引号就
array_walk_recursive($a, function(&$v) {$v= intval($v);});echo json_encode($a);
Copy after login
{"data":[0,0,0,0,4,0],"result":0}
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template