Output JSON entry with multidimensional array—YAML adds extra syntax

Playing for the first time with json today.

I’ve been able to produce this kind of json output so far:

{
  "title": "Biblioteca di Laives",
  "zip": "39055",
  "city": "Leifers, Bozen",
  "location": [
      {
          "lat": "46.428279",
          "long": "11.340092"
      }
  ]
},
{
  ...
}

But I need the json output to be like this, notice the location field:

{
  "title": "Biblioteca di Laives",
  "zip": "39055",
  "city": "Leifers, Bozen",
  "location": [
      46.428279,
      11.340092
  ]
},
{
  ...
}

I’m using a YAML structured field to store the two values in an array

Location: 

- 
  lat: "46.314294"
  long: "11.272767"

----

and when I output this field to json I do this

$json[] = array(
    'title' => (string)$place->title(),
    'zip' => (string)$place->zip(),
    'city' => (string)$place->city(),
    'location' => $place->location()->yaml()
  );

Is there a way to get rid of the extra syntax when using a YAML field?

The yaml() method returns a multidimensional array, you would have to “flatten” it first.

Thank you, I made it work!


For future reference:

  • Make a function that flatten an array and take only values and not keys
// https://davidwalsh.name/get-array-values-with-php-recursively

<?php
function array_values_recursive($array) {
  $flat = array();

  foreach($array as $value) {
    if (is_array($value)) {
        $flat = array_merge($flat, array_values_recursive($value));
    }
    else {
        $flat[] = $value;
    }
  }
  return $flat;
}

$location = array_values_recursive($place->location()->yaml());
  • Transform numbers to floats
// $location is an array built with a YAML structured field like this
// page.txt
//
// Location: 
//
// - 
//  lat: "46.428279"
//  long: "11.340092"

 $location = array_map('floatval', $location);

Complete syntax:

<?php
header('Content-type: application/json; charset=utf-8');

$places = $pages->find('luoghi')->children()->visible();

$data = $places;
$json = array();

// https://davidwalsh.name/get-array-values-with-php-recursively
function array_values_recursive($array) {
  $flat = array();

  foreach($array as $value) {
    if (is_array($value)) {
        $flat = array_merge($flat, array_values_recursive($value));
    }
    else {
        $flat[] = $value;
    }
  }
  return $flat;
}

foreach($data as $place) {

  $location = array_values_recursive($place->location()->yaml());
  $location = array_map('floatval', $location);

  $json[] = array(
    'url'   => (string)$place->url(),
    'title' => (string)$place->title(),
    'street'  => (string)$place->street(),
    'zip' => (string)$place->zip(),
    'city' => (string)$place->city(),
    'location' => $location
  );

}

echo json_encode($json, JSON_PRETTY_PRINT);

Final output will be

{
  "url": "http:\/\/artoteca.dev\/luoghi\/biblioteca-di-egna",
  "title": "Biblioteca di Egna",
  "street": "Franz - Bonatti - Platz, 24",
  "zip": "39044",
  "city": "Neumarkt, Bozen, Itali\u00eb",
  "location": [
      46.314294,
      11.272767
  ]
},