首頁 >後端開發 >php教程 >json转数组

json转数组

WBOY
WBOY原創
2016-06-20 12:34:45935瀏覽

{"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);

{"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}

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn