在phpmony中将php数组转换为json格式

时间:2014-07-01 02:04:41

标签: php json symfony

我有这个Json(见下文),我正在尝试找到一种使用php数组生成这样的方法。我的问题是如何从php数组生成这样的json。换句话说,我仍然不知道如何构建那个php数组,以便我可以将它转换为这个json,如下所示。我正在使用symfony2来渲染这个像这样的json

var presentation = [{
                    "image": "images/cover.jpg",
                    "sentence": "This is a sentence",
                    "audio": "hello.mp3",
                    "sentence_info": [
                        {"start": 0, "end": 0.5 },
                        { "start": 0.5, "end": 1.2 }
                    ]
                },

                {
                    "image": "images/cat.jpg",
                    "sentence": "This is another sentence",
                    "audio": "bey.mp3",
                    "sentence_info": [
                        { "start": 0, "end": 0.2 },
                        { "start": 0.2, "end": 0.8 },
                        { "start": 0.8, "end": 1.2 },
                        { "start": 1.2, "end": 1.5 },
                        { "start": 1.5, "end": 2 }
                    ]
                },

                {
                    "image": "images/dog.jpg",
                    "sentence": "This is a third sentence",
                    "audio": "good.mp3",
                    "sentence_info": [
                        { "start": 0, "end": 0.5 },
                        { "start": 0.5, "end": 1.2 }
                    ]
                }
];

3 个答案:

答案 0 :(得分:1)

这只是PHP中的简单数组赋值。像这样:

public function whatEver()
{
    $presentation = array(
        array(
            'image' => 'images/cover.jpg',
            'sentence' => 'This is a sentence',
            'audio' => 'hello.mp3',
            'sentence_info' => array(
                array('start' => 0, 'end' => 0.5),
                array('start' => 0.5, 'end' => 1.2),
            ),
        ),
        array(
            'image' => 'images/cat.jpg',
            'sentence' => 'This is another sentence',
            'audio' => 'bey.mp3',
            'sentence_info' => array(
                array('start' => 0, 'end' => 0.2),
                array('start' => 0.2, 'end' => 0.8),
                array('start' => 0.8, 'end' => 1.2),
                array('start' => 1.2, 'end' => 1.5),
                array('start' => 1.5, 'end' => 2),
            ),
        ),
        array(
            'image' => 'images/dog.jpg',
            'sentence' => 'This is a third sentence',
            'audio' => 'good.mp3',
            'sentence_info' => array(
                array('start' => 0, 'end' => 0.5),
                array('start' => 0.5, 'end' => 1.2),
            ),
        ),
    );

    $presentation =  json_encode($presentation, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

    $response = new Response($presentation);
    $response->headers->set('Content-Type', 'application/json');

    return $response;
}

答案 1 :(得分:0)

您可以在已构建的阵列上使用json_encode

答案 2 :(得分:0)

简单地看看提供的json你应该知道它是一个数组 [] ,里面有各种对象 {}

您可以从任何地方(很可能是数据库?)循环遍历数据,然后将其添加到数组中,如下所示:

$data = array();
foreach ($data_from_wherever as $item) {
    $i = array(
        'image' => '',
        'sentence' => '',
        'audio' => '',
        'sentence_info' => array(
            array(
                'start' => '',
                'finish' => ''
            ),
            array(
                'start' => '',
                'finish' => ''
            ),
        // ......etcetc
        ),
    );

    array_push($data, $i);
}

为您提供一个阵列,您json_encode()可以实现所需的输出。

$presentation = json_encode($data);

如果您还不知道如何获取所述数据,您只需创建如下所示的$data数组,然后对其进行编码。

$data = array(
    array(
        'image' => 'images/cover.jpg',
        'sentence' => 'This is a sentence',
        'audio' => 'hello.mp3',
        'sentence_info' => array(
            array('start' => 0, 'end' => 0.5),
            array('start' => 0.5, 'end' => 1.2),
        ),
    ),
    array(
        'image' => 'images/cat.jpg',
        'sentence' => 'This is another sentence',
        'audio' => 'bey.mp3',
        'sentence_info' => array(
            array('start' => 0, 'end' => 0.2),
            array('start' => 0.2, 'end' => 0.8),
            array('start' => 0.8, 'end' => 1.2),
            array('start' => 1.2, 'end' => 1.5),
            array('start' => 1.5, 'end' => 2),
        ),
    ),
    array(
        'image' => 'images/dog.jpg',
        'sentence' => 'This is a third sentence',
        'audio' => 'good.mp3',
        'sentence_info' => array(
            array('start' => 0, 'end' => 0.5),
            array('start' => 0.5, 'end' => 1.2),
        ),
    ),
);