这是我构建动态json的查询
$Query = "SELECT url as src, notes as text, `x-axis` as x, `y-axis` as y, width as width, height as height FROM annotate where `url` ='".$url."' limit 0,10 ";
$Result = $Connection->query($Query);
$Data = $Result->fetch_assoc();
$result=array();
$i=0;
while($row = $Result->fetch_assoc()){
$result[$i]['src']=$row['src'];
$result[$i]['text']=$row['text'];
$result[$i]['shapes']['type']= 'rect';
$result[$i]['shapes']['geometry'] =array('x' => $row['x'], 'y'=> $row['y'], 'width' => $row['width'], 'height'=>$row['height'] );
$i++;
}
echo json_encode($result);
这是预期输出和实际输出....
第一个是预期数据的控制台(我将控制台设置为静态)实际输出是第二个。
以下是用于控制静态输出的变量。
var my = {
src : 'http://192.168.1.58/annotate/drive/image/<?php echo $_GET['file']?>',
text : 'Suresh and Gopinath....',
shapes : [{
type : 'rect',
geometry : { x : 0.1825726141078838, y: 0.23756906077348067, width : 0.11602209944751381, height: 0.11618257261410789 }
}]
}
如何将形状与第一个一样形成数组?
注意:
这个问题是this one
的延续答案 0 :(得分:1)
试试这个:
$Result = $Connection->query($Query);
$result=array();
$i=0;
while($row = $Result->fetch_assoc()){
$result[$i]['src']=$row['src'];
$result[$i]['text']=$row['text'];
$result[$i]['shapes'][]=array('type'=>'rect','geometry'=>array('x' => $row['x'], 'y'=> $row['y'], 'width' => $row['width'], 'height'=>$row['height']) );
$i++;
}
echo json_encode($result);