Building json array inside while loop

Here is my request for building dynamic 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);

Here is the expected conclusion and the actual result ....

The first of these is the expected data console (I set the console as static), the actual output is the second.

The following is the variable used to consolidate static output.

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 }
    }]
}

How can I make shapes as an array like the first?

enter image description here

Note:

These questions are a continuation of this.

+4
source share
1 answer

Try the following:

$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);
+1
source