在symfony中将php数组转换为json格式[关闭]

问题描述:

I have this Json(see below) and I am trying actually to find a way to generate one like this using a php array. My question is how can I generate a json like this from a php array. in another way, I still don't know how to build that php array so that I can convert it to this a json like the one below. I am using symfony2 to render this a json like this one

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 }
                    ]
                }
];

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

  var presentation = [{
“image”:“images / cover.jpg”,
“句子”  :“这是一个句子”,
“音频”:“hello.mp3”,
“sentence_info”:[
 {“start”:0,“end”:0.5},
 {“start”:  0.5,“结束”:1.2} 
] 
},
 
 {
“image”:“images / cat.jpg”,
“句子”:“这是另一句话”,
“  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“,
”句子“:”这是第三句“,
”音频“:”good.mp3“,
”sentence_info“:[
 {”start“  :0,“结束”:0.5},
 {“开始”:0.5,“结束”:1.2} 
] 
} 
]; 
  code>  pre> 
  div  >

This is just simple array assignment in PHP. Like this:

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;
}

You would use json_encode on an array you've built up.

A simple look at the supplied json would should you that it is an array [] with various objects {} inside.

You'd be looping through data from wherever you get it from (most likely a database?) and then adding it to the array like this:

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

Giving you an array that you'd json_encode() to acheive your desired output.

$presentation = json_encode($data);

And if you don't know how to get said data yet, you can simply create the $data array like below and then encode it.

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