1 回答

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊
首先,您的 JSON 數(shù)據(jù)無效。確保您有有效的 json 字符串。您可以使用JSONLint等工具來驗(yàn)證 JSON 數(shù)據(jù)。從那里,您可以使用json_decode進(jìn)行解析,然后構(gòu)建您想要的任何數(shù)組結(jié)構(gòu)。例如,
//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 關(guān)注
- 144 瀏覽
添加回答
舉報(bào)