1 回答

TA貢獻1946條經驗 獲得超4個贊
首先,您的 JSON 數據無效。確保您有有效的 json 字符串。您可以使用JSONLint等工具來驗證 JSON 數據。從那里,您可以使用json_decode進行解析,然后構建您想要的任何數組結構。例如,
//valid JSON
$json = '[{"row":0,"col":0,"value":4.5},{"row":0,"col":1,"value":4.3},{"row":0,"col":2,"value":4.9},{"row":1,"col":1,"value":3.1}]';
//parse json data
$data = json_decode( $json );
//a new array to hold the parsed data
$parsed = [];
//iterate over the json data and re-structure as desired
foreach( $data as $item ) {
? ? //do we have a place for this row yet?
? ? if( ! isset($parsed[$item->row]) ) {
? ? ? ? //no array for this row, create an empty one
? ? ? ? $parsed[ $item->row ] = [];
? ? }
? ? //set the value for this column
? ? $parsed[ $item->row ][ $item->col ] = $item->value;
}
的輸出$parsed將是:
Array
(
? ? [0] => Array
? ? ? ? (
? ? ? ? ? ? [0] => 4.5
? ? ? ? ? ? [1] => 4.3
? ? ? ? ? ? [2] => 4.9
? ? ? ? )
? ? [1] => Array
? ? ? ? (
? ? ? ? ? ? [1] => 3.1
? ? ? ? )
)
- 1 回答
- 0 關注
- 159 瀏覽
添加回答
舉報