JSON數據我有以下格式格式化在PHP
["0","0","0","0","0","0","0","2","5","4","3","0"]
JSON數據我想上面的數據轉換爲使用PHP
[0,0,0,0,0,0,0,2,5,4,3,0]
以下格式我怎樣才能做到這一點使用PHP
感謝
JSON數據我有以下格式格式化在PHP
["0","0","0","0","0","0","0","2","5","4","3","0"]
JSON數據我想上面的數據轉換爲使用PHP
[0,0,0,0,0,0,0,2,5,4,3,0]
以下格式我怎樣才能做到這一點使用PHP
感謝
您可以使用array_map將數組中的所有項類型化爲具有回調intval的整數。
對於一個整數數組:
$array = array_map('intval', json_decode('["0","0","0","0","0","0","0","2","5","4","3","0"]'));
從陣列中檢索JSON:
echo json_encode($array);
非常感謝! – salloy
@salloy如果它完全解答了您的問題,請將我的答案標記爲您的問題的答案;) – RichardBernards
$string = '["0","0","0","0","0","0","0","2","5","4","3","0"]';
echo str_replace('"','', $string);
輸出爲:
[0,0,0,0,0,0,0,2,5,4,3,0]
你想要什麼輸出DO?一個字符串,一個數組? – BastienSander
事實上,最初我有一個數組在PHP之後,我已經將數組轉換爲json使用json_encode函數之後,它返回一個字符串包含值的JSON表示如下[「0」,「0」,「0」,「 0「,」0「,」0「,」0「,」2「,」5「,」4「,」3「,」0「],現在我想要相同的字符串,但值不應包含雙逗號「」) – salloy
你的問題是,這些數組中的項目被認爲是你的PHP中的字符串...你可以在我的答案中用array_map('intval',array())例子來修復它。當PHP中的事物被認爲是字符串時,json_encode()方法在值的兩端添加雙引號 – RichardBernards