简体   繁体   English

转换嵌套的JSON数组

[英]Converting nested JSON array

I have a PHP function that builds a JSON array via 我有一个PHP函数可以通过构建一个JSON数组

$jsonArray= array();
for ($i=0; $i<$dirCount; $i++){
  $query = sprintf("SELECT * FROM tour WHERE FileName= '../%s/%s'", $imageDirectory, $dirArrays[$i]);
  $result = mysqli_query($link, $query);

  if (mysqli_num_rows($result) == 1){
    $row = mysqli_fetch_row($result);
    $jsonArray[]= array('filename'=>$dirArrays[$i], 'location'=>$row[4], 'latitude'=>$row[2], 'longitude'=>$row[3], 'heading'=> $row[5]);
  } 
}

and returns it upon execution via an ajax query. 并在执行时通过ajax查询返回它。

However, it is shown in Firebug as 但是,它在Firebug中显示为

[
  0 : Object{ 'filename' : , 'location': , 'latitude': , 'longitude: },
  1 : Object{ 'filename' : , 'location': , 'latitude': , 'longitude: }, 
]

and so on 等等

How can I convert this so that the index locations are the location value instead? 如何转换此值,以便索引位置改为location值? What I have in mind is 我想到的是

'start' : Object{ 'filename' : , 'location': , 'latitude': , 'longitude: },
'testLab' : { 'filename' : , 'location': , 'latitude': , 'longitude: }

The reason behind this is I have another function that creates an object with the data fields upon a match to the location field. 这背后的原因是我还有另一个函数,该函数在与location字段匹配时使用数据字段创建对象。

function buildData(input){
  for (var i=0; i<data.length; i++){
    if (data[i].location == input)
      //create and return object using data[i] fields
  } 
}

I'd like get rid of the loop and rely on the conditional like 我想摆脱循环,依靠有条件的像

function buildData(input){
  if (data[input]){
    //same object creation and return
  }
}

How would this be done? 怎么做?

不仅仅是分配数组的每个元素( $jsonArray[] = ... ),而是分配给相关的键( $jsonArray[$somekey] = ... )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM