2

I'm outputting some JSON from PHP, but I'm having difficulty understanding how to do nested arrays (at least I think that is what it is called)

I can output single sets, for example, "type": "Feature" but how would I do

"geometry": {
    "type": "Point",
    "coordinates": [-77.03238901390978,38.913188059745586]
},

For example, the desired output for one item in the JSON array might be:

{
    "type": "Feature",
    "geometry": {
        "type": "Point",
        "coordinates": [-77.03238901390978,38.913188059745586]
    },
    "properties": {
        "title": "Mapbox DC",
        "description": "1714 14th St NW, Washington DC",
        "marker-color": "#fc4353",
        "marker-size": "large",
        "marker-symbol": "monument"
    }
},

And my code so far looks like this:

<?php 
$projects = $pages->find('template=project-detail, sort=sort');
$projects_array = array();

foreach ($projects as $project) {

    $title = $project->title;
    $long = $project->project_location_marker_long;
    $lat = $project->project_location_marker_lat;

    $projects_array[] = array(
        'title' => $title
    );

}

$projects_json = json_encode($projects_array, true);
?>
<script>
var geojson = <?php echo echo $projects_json; ?>
</script>

Which generates something like the following:

[{
    "title": "Steel Strike 1980"
}, {
    "title": "Chapel Flat Dyke Boat"
}]
1
  • Why do you have true as a second argument to json_encode? The second argument for that function is an option indended to be filled by a constant. See php.net/manual/en/function.json-encode.php for more info. Perhaps you're thinking of json_decode, which takes a second agrument to return an associative array. Commented May 13, 2015 at 20:38

3 Answers 3

3

A nested array is simple to create. Here is one example:

$my_array = array(
    'string_example' => 'asdf',
    'integer_example' => 42,
    'array_example' => array() // this array is nested
);

Inside this nested array, you could put anything you'd like. For instance, let's put the exact same thing in it:

$my_array = array(
    'string_example' => 'asdf',
    'integer_example' => 42,
    'array_example' => array(
        'string_example' => 'asdf',
        'integer_example' => 42,
        'array_example' => array()
    )
);

So working from your code example, here is a start, given the data you included:

foreach ($projects as $project) {

    $title = $project->title;
    $long = $project->project_location_marker_long;
    $lat = $project->project_location_marker_lat;

    $projects_array[] = array(
        'geometry' => array(
            'coordinates' => array($long, $lat)
        )
        'properties' => array(
            'title' => $title
        )
    );

}

This will result in the following json when encoded:

{
    "geometry": {
        "coordinates": [-77.03238901390978,38.913188059745586]
    },
    "properties": {
        "title": "Mapbox DC",
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

There's a simple way to figure this out. Just take your example JSON, decode it and see what the output looks like:

<?php
$json = '
{
    "type": "Feature",
    "geometry": {
        "type": "Point",
        "coordinates": [-77.03238901390978,38.913188059745586]
    },
    "properties": {
        "title": "Mapbox DC",
        "description": "1714 14th St NW, Washington DC",
        "marker-color": "#fc4353",
        "marker-size": "large",
        "marker-symbol": "monument"
    }
}';
var_export(json_decode($json, true));

Output:

array (
  'type' => 'Feature',
  'geometry' => 
  array (
    'type' => 'Point',
    'coordinates' => 
    array (
      0 => -77.032389013909778,
      1 => 38.913188059745586,
    ),
  ),
  'properties' => 
  array (
    'title' => 'Mapbox DC',
    'description' => '1714 14th St NW, Washington DC',
    'marker-color' => '#fc4353',
    'marker-size' => 'large',
    'marker-symbol' => 'monument',
  ),
)

Comments

0

if you like to encode for example the lat/lon of your sample code it would be:

$title = $project->title;
$long = $project->project_location_marker_long;
$lat = $project->project_location_marker_lat;

$projects_array[] = array(
    'title' => $title,
    'coordinates' => array($lat,$lon)
);

this will result in something like this:

[{
    "title": "Steel Strike 1980",
    "coordinates": [-77.03238901390978,38.913188059745586]
}, {
    "title": "Chapel Flat Dyke Boat",
    "coordinates": [-77.03238901390978,38.913188059745586]
}]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.