使用非关联数组包装关联数组,以便JavaScript获得正确的格式

时间:2014-10-12 22:41:35

标签: javascript php drupal

我目前正在使用drupal和Flowplayer API。

在Javascript中,对我来说一切都很简单,但是当涉及到PHP时,我的noob知识还不够。

我需要使流程图插件工作是一个像这样的javascript结构:

                             "ads" : {
                                    "schedule" : [ {
                                        "position" : "pre-roll",
                                        "server" : {
                                            "type" : "direct",
                                            "timeoutInSeconds" : 5,
                                            "tag" : "http%3Asomething",
                                        }
                                    } ]
                                }

但是使用我的php API

            'ads'=> array(
                  'scheudle'=>array(
                      'position'=>'pre-roll',
                      'server'=> array(
                          'type'=>'direct',
                          "timeoutInSeconds"=>5,
                          "tag"=>"httpsomething",
                      )

                  )
              )

我得到这样的JavaScript:

 "ads":{
    "scheudle":{  /*HERE NEEDS TO BE A WRAPPING [] ARRAY LIKE ABOVE */
       "position":"pre-roll",
       "server":{
           "type":"direct",
           "timeoutInSeconds":5,
           "tag":"http%3something"
       }
    }
},

如何让我的PHP输出正确的js结构?

1 个答案:

答案 0 :(得分:2)

使用此:

'ads'=> array(
                  'scheudle'=>array(array(
                      'position'=>'pre-roll',
                      'server'=> array(
                          'type'=>'direct',
                          "timeoutInSeconds"=>5,
                          "tag"=>"httpsomething",
                      )

                  ))
              )