duanchi19820419 2013-08-28 15:02
浏览 511
已采纳

如何删除一些包装JSON数组的方括号

I am trying to workout how to format the brackets that are wrapping my JSON array. I am pulling the data for my array out of my database using this code.

 $result = mysqli_query($con,"SELECT * FROM table");
 $column = array();
  while ($row = mysqli_fetch_array($result))
  {
   $column[] = array(array($row[0],$row[1],$row[2],$row[3],$row[4],$row[5]),$row[3],$row[2],$row[0]);
  }

Once converted to JSON the Array prints to the screen in this format. I need the first square brackets to remain but need to swap the second set of brackets for (\').

var myVar = [
[["22","mike"," 151.1272","-33.9324","learning to fish","08\/14\/13"],"-33.93024"," 151.12896172","22"],
[["23","mike"," -2.09875","51.44501","learning french","08\/04\/13"],"51.44501"," -2.09775","23"],
[["24","mike"," -2.16375","51.44823019014051","Programming","08\/05\/13"],"51.451"," -2.1675","24"],
];

So the Array should look more like this.

var myVar = [
['"22","mike"," 151.12","-33.934","learning to fish","08\/14\/13"',"-33.94"," 151.12","22"],
['"23","mike"," -2.095","51.41","learning french","08\/04\/13"',"51.41"," -2.095","23"],
['"24","mike"," -2.165","51.41","Programming","08\/05\/13"',"51.44851"," -2.1675","24"],
 ];

The reason I need the Array in this format is so that I can pass the data into Google Maps function which expects the Array to have 4 columns.

I have worked out I can remove the brackets with a function like this, but the problem is it removes all the brackets and I need the external brackets;

$js_array = str_replace(array('[', ']'), '\'', htmlspecialchars(json_encode($column), ENT_NOQUOTES));

How would I keep the external brackets and simply remove the internal brackets? If I have not given enough information or I should add any more details let me know.

Thanks

  • 写回答

2条回答 默认 最新

  • douzhuoxia0587 2013-08-28 15:07
    关注

    How about making your array the way you need it in the first place? Instead of:

    $column[] = array(
        array(
            $row[0],
            $row[1],
            $row[2],
            $row[3],
            $row[4],
            $row[5]
        ),
        $row[3],
        $row[2],
        $row[0]
    );
    

    Do this:

    $column[] = array(
        $row[0] . $row[1] . $row[2] . $row[3] . $row[4] . $row[5],
        $row[3],
        $row[2],
        $row[0]
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败