【问题标题】:Constructing json array inside while loop在while循环中构造json数组
【发布时间】:2015-11-25 05:10:06
【问题描述】:

这是我构造动态 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的延续

【问题讨论】:

  • 好吧,shapes 是一个对象数组。 Have a look here
  • @Crecket 我也做了同样的事情..
  • @Andrew 谢谢,现在会参考它

标签: php jquery mysql arrays json


【解决方案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);

【讨论】:

    猜你喜欢
    • 2012-10-31
    • 1970-01-01
    • 2019-12-06
    • 2017-05-19
    • 2018-10-12
    • 2020-10-28
    • 1970-01-01
    • 2020-12-01
    • 2015-02-20
    相关资源
    最近更新 更多