0

I have a PHP function that builds a JSON array via

$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.

However, it is shown in Firebug as

[
  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? 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.

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?

1 Answer 1

4

Instead of just pushing each element of the array ($jsonArray[] = ...), just assign to the relevant key ($jsonArray[$somekey] = ...).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.