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 关注
- 125 浏览
添加回答
举报