2

I'm trying to get the following formatting in JSON using PHP which is querying the database... notice there is no final comma

[
      {
        "ID": "4",
        "Name": "Jill Higgins",
        "Job Title": "Designer",
        "Organisation": "Design Widget",
        "Organisation Type": "Academia",
        "Latitude": "54.669978",
        "Longitude": "-1.452469"
    },
    {
        "ID": "5",
        "Name": "Bob Billy",
        "Job Title": "Clown",
        "Organisation": "Big Comp INC",
        "Organisation Type": "Company",
        "Latitude": "54.669978",
        "Longitude": "-1.452469"
    }
]

This is my code currently...

if (PerchUtil::count($members)) {

echo '[';

foreach ($members as $Member) {
  //prepare the data
  $data = array(
    'ID' => $Member->memberID(),
    'Name' => $Member->first_name() . ' ' . $Member->last_name(),
    'Job Title' => $Member->expert_job_title(),
    'Organisation' => $Member->expert_org_name(),
    'Organisation Type' => $Member->expert_org_type(),
    'Latitude' => $Member->expert_org_latitude(),
    'Longitude' => $Member->expert_org_longitude()
  );
}
echo ']';
}

header('Content-Type: application/json');

This is what it currently looks like... notice there is a comma at the end which I don't need. The spacing isn't really helpful with the brackets either... how do I amend the PHP so it is cleaner and counts out the final comma?

[{
    "ID": "4",
    "Name": "Jill Higgins",
    "Job Title": "Designer",
    "Organisation": "CPI",
    "Organisation Type": "Academia",
    "Latitude": "54.669978",
    "Longitude": "-1.452469"
},{
    "ID": "5",
    "Name": "Bob Billy",
    "Job Title": "Clown",
    "Organisation": "Big Comp INC",
    "Organisation Type": "Company",
    "Latitude": "54.669978",
    "Longitude": "-1.452469"
},]
9
  • 4
    Build the data into an array (something like $data[] = ) and then json_encode() the result Commented Jun 26, 2018 at 14:22
  • How do I do this? Commented Jun 26, 2018 at 14:24
  • 1
    You should not manually manipulate json strings like this, what are you trying to make it pretty for? Some display purpose? Commented Jun 26, 2018 at 14:24
  • Readability. To clarify are you referencing the echo statments? Commented Jun 26, 2018 at 14:25
  • You can pass the JSON_PRETTY_PRINT flag, read the manual for json_encode Commented Jun 26, 2018 at 14:26

1 Answer 1

6

Create an array of the data you want to encode in $data, add each item using $data[] = and then echo json_encode($data); to get the right format...

if (PerchUtil::count($members)) {
    $data = [];
    foreach ($members as $Member) {
      //prepare the data
      $data[] = array(
         'ID' => $Member->memberID(),
         'Name' => $Member->first_name() . ' ' . $Member->last_name(),
         'Job Title' => $Member->expert_job_title(),
         'Organisation' => $Member->expert_org_name(),
         'Organisation Type' => $Member->expert_org_type(),
         'Latitude' => $Member->expert_org_latitude(),
         'Longitude' => $Member->expert_org_longitude()
      );
    }
    echo json_encode($data, JSON_PRETTY_PRINT);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Works well - so $data[] is an empty array?
When assigning to $data[] - your assigning the value to the next element in the array.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.