加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 在循环中构造json数组

发布时间:2020-12-13 15:57:45 所属栏目:PHP教程 来源:网络整理
导读:这是我构建动态json的Query $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_
这是我构建动态json的Query

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

我怎样才能像第一个那样将形状作为数组?

enter image description here

注意 :

这个问题是this one的延续

解决方法

试试这个:

$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'],'height'=>$row['height']) ); 
$i++; 
} 
echo json_encode($result);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读