将PHP数组格式化为JSON数组

时间:2014-06-19 13:21:47

标签: php arrays json

是否有可能从PHP数组到JSON数组?如果是这样,有人可以进一步解释一下吗?

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'
        }
    }
],

这是我到目前为止所做的:

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

   echo json_encode($json);
?>

{"title":"Test event","location":"Test location"}

3 个答案:

答案 0 :(得分:2)

你走在正确的轨道上,但为了更进一步,你必须使它成为多维的(你可以把它写成一个对象,但我更喜欢数组):

<?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"}}

那应该没问题

答案 1 :(得分:1)

您可以使用json_encode()JSON_PRETTY_PRINT选项:

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

   echo json_encode($json, JSON_PRETTY_PRINT);
?>

修改

  

我如何开始{},结束{}区域,并将其变成数组?

您可以像这样定义变量:

$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'
      )
    )
  )
);

输出:

{
    "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"
            }
        }
    ]
}

答案 2 :(得分:0)

Json_encode也适用于数组:

从手册(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

修改 如前所述,您可能正在寻找格式选项: http://www.php.net/manual/en/json.constants.php

特别是:JSON_PRETTY_PRINT