Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is it possible to make this, from a PHP array, to a JSON array? If so, could someone explain a little further?

events: [
    {
        title: 'Test event',
        location: 'Test location',
        start: {
            date: '20140607', time: '17.00'
        },
        end: {
            date: '20140623', time: '17.00'
        }
    },
    {
        title: 'Test event',
        location: 'Test location',
        start: {
            date: '20140607', time: '17.00'
        },
        end: {
            date: '20140623', time: '17.00'
        }
    }
],

This is what I have so far:

<?php
   $json->title = 'Test event';
   $json->location  = 'Test location';

   echo json_encode($json);
?>

{"title":"Test event","location":"Test location"}
share|improve this question
    
? this question is very vague and you already got what you wanted ? –  EaterOfCode Jun 19 at 13:23
1  
Is there something wrong with your JSON when you use json_encode()? –  Jay Blanchard Jun 19 at 13:23
    
I think he wants a pretty formatting function. –  Elzo Valugi Jun 19 at 13:24
    
Sorry! How would I do the start { }, end {} areas, and make it into an array? –  braw Jun 19 at 13:26
    
Just build an array of objects in php and then encode it. –  Mike Brant Jun 19 at 13:28

3 Answers 3

up vote 2 down vote accepted

You are on the right track but to go further you have to make it multidimensional (You can write it as an object but I prefer arrays):

<?php
$jsonArr['title'] = 'Test event';
$jsonArr['location']  = 'Test location';

$jsonArr['start']['date']= '20140607';
$jsonArr['start']['time']= '17.00';

$jsonArr['end']['date']= '20140607';
$jsonArr['end']['time']= '17.00';

echo json_encode($jsonArr);

//Or an object example:

$json->title = 'Test event';
$json->location  = 'Test location';

$json->start->date= '20140607';
$json->start->time= '17.00';

$json->end->date= '20140607';
$json->end->time= '17.00';

echo json_encode($json);

?>

{"title":"Test event","location":"Test location","start":{"date":"20140607","time":"17.00"},"end":{"date":"20140607","time":"17.00"}}

That should do you fine

share|improve this answer
    
Thanks so much – this makes sense. –  braw Jun 19 at 13:33
    
I meant to ask, what about multiple events? And could you show an object example? Just trying to get my head around it. –  braw Jun 19 at 13:39
    
What do you mean by multiple events? I will edit an object example in –  JammyDodger231 Jun 19 at 13:42
    
Thank you! So, if I was to include this in a JS script function, would this be the best way to do it? snippi.com/s/8z9wq4t –  braw Jun 19 at 13:51
    
That is the best way as far as I know –  JammyDodger231 Jun 19 at 13:57

You can use the JSON_PRETTY_PRINT option of json_encode():

<?php
   $json = new stdClass();
   $json->title = 'Test event';
   $json->location  = 'Test location';

   echo json_encode($json, JSON_PRETTY_PRINT);
?>

Edit:

How would I do the start { }, end {} areas, and make it into an array?

You can define the variable like this:

$json = array(
  'events' => array(
    array(
      'title' => 'Test event',
      'location' => 'Test location',
      'start' => array(
        'date' => '20140607',
        'time' => '17.00'
      ),
      'end' => array(
        'date' => '20140623',
        'time' => '17.00'
      )
    ),
    array(
      'title' => 'Test event',
      'location' => 'Test location',
      'start' => array(
        'date' => '20140607',
        'time' => '17.00'
      ),
      'end' => array(
        'date' => '20140623',
        'time' => '17.00'
      )
    )
  )
);

It outputs:

{
    "events": [
        {
            "title": "Test event",
            "location": "Test location",
            "start": {
                "date": "20140607",
                "time": "17.00"
            },
            "end": {
                "date": "20140623",
                "time": "17.00"
            }
        },
        {
            "title": "Test event",
            "location": "Test location",
            "start": {
                "date": "20140607",
                "time": "17.00"
            },
            "end": {
                "date": "20140623",
                "time": "17.00"
            }
        }
    ]
}
share|improve this answer
    
Thank you! So, if I was to include this in a JS script function, would this be the best way to do it? snippi.com/s/8z9wq4t –  braw Jun 19 at 13:50
    
If the events come from the database, then yes. But if data is static (included in the source file), then you can do it without PHP. In that case just use JavaScript: jsfiddle.net/r2nAe/1 –  Gergo Erdosi Jun 19 at 13:56
    
Events are from the DB so that's good to know. Thanks! –  braw Jun 19 at 13:57

Json_encode also works on arrays:

From the manual (http://www.php.net//manual/en/function.json-encode.php):

<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

echo json_encode($arr);
?>

Ideone: http://ideone.com/rD2Yvg

EDIT: As pointed out earlier, you may be looking for formatting options: http://www.php.net/manual/en/json.constants.php

Especially: JSON_PRETTY_PRINT

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.