Wednesday, 15 February 2012

Format a PHP array to JSON array -



Format a PHP array to JSON array -

is possible create this, php array, json array? if so, explain 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 have far:

<?php $json->title = 'test event'; $json->location = 'test location'; echo json_encode($json); ?> {"title":"test event","location":"test location"}

you on right track go farther have create multidimensional (you can write object 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 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 fine

php arrays json

No comments:

Post a Comment