0

我有一个 PHP 函数,它通过

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

并在通过 ajax 查询执行时返回它。

但是,它在 Firebug 中显示为

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

等等

如何转换它以便索引位置是location值?我的想法是

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

这背后的原因是我有另一个函数,它在与位置字段匹配时创建一个带有数据字段的对象。

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

我想摆脱循环并依赖条件

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

这将如何完成?

4

1 回答 1

4

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

于 2012-06-29T01:35:21.277 回答