doujiepin5547 2014-07-01 02:04
浏览 81
已采纳

在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 }
                    ]
                }
];
  • 写回答

3条回答 默认 最新

  • dongshanxun6479 2014-07-01 02:30
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作