json轉數組

來源:互聯網
上載者:User
{"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}
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.