通过从 PHP 传入数组,在 java 脚本中填充项目数组及其值


Populating an array of items and its values in java script by passing in an array from PHP

<?php
$myarray = array();  
<script type="text/javascript">
    $(function(){
        $("#demo").grid({
            'genre' : 'All',
            'items' :
            [
                {
                    'title'         : 'A',
                    'description'   : 'qwerty',
                    'thumbnail'     : ['http://mypd.front.net/f4534.jpg'],
                    'large'         : ['http://mypd.front.net/f4534_large.jpg'],
                    'button_list'   :
                    [
                        { 'title':'Demo', 'url' : 'http://mypd.front.net/', 'new_window' : true }
                    ],
                    'tags'          : ['Movies']
                },
                {
                    'title'         : 'B',
                    'description'   : 'asdfg',
                    'thumbnail'     : ['http://mypd.front.net/f5534.jpg'],
                    'large'         : ['http://mypd.front.net/f5534_large.jpg'],
                    'button_list'   :
                    [
                        { 'title':'Demo', 'url' : 'http://mypd.front.net/', 'new_window' : true }
                    ],
                    'tags'          : ['Series']
                },
                ...
                ...
                ...
                {
                    'title'         : 'Z',
                    'description'   : 'poiyut',
                    'thumbnail'     : ['http://mypd.front.net/f6534.jpg'],
                    'large'         : ['http://mypd.front.net/f6534_large.jpg'],
                    'button_list'   :
                    [
                        { 'title':'Demo', 'url' : 'http://mypd.front.net/', 'new_window' : true }
                    ],
                    'tags'          : ['Movies', 'Series']
                }
            ]
        });
    });
</script>

这是我的旧索引.php中的嵌入式脚本。

现在我想做的是通过从 php 文件中的数组中读取数据来填充项目数组(如标题、描述等)中的数据。

你能告诉我我该怎么做吗?

像这样的东西是我正在寻找的?

for ($i = 0; $i < sizeof($vodAssetArray); $i++)  
{  
   populate each of the items in the java script like   
     {
                    'title'         : $vodAssetArray[i]->title,
                    'description'   : $vodAssetArray[i]->description,
                    'thumbnail'     : [$vodAssetArray[i]->imgSrc],
                    'large'         : [$vodAssetArray[i]->imgSrc],
                    'button_list'   :
                    [
                        { 'title':'Demo', 'url' : 'http://mypd.front.net/', 'new_window' : true }
                    ],
                    'tags'          : [$vodAssetArray[i]->genre]
                },
}  
?>

示例

<?php
$vodAssetArray = array(
    array(
        'title'         =>'title 1',
        'description'   =>'description 1',
        'thumbnail'     =>'thumbnail imgSrc 1',
        'large'         =>'imgSrc 1',
        'genre'          =>'Tags 1',
    ),
    array(
        'title'         =>'title 2',
        'description'   =>'description 2',
        'thumbnail'     =>'thumbnail imgSrc 2',
        'large'         =>'imgSrc 2',
        'genre'          =>'Tags 2',
    )
);
$javascript= array();
$count = sizeof($vodAssetArray);
for($i = 0; $i < $count;  $i++)
{
$javascript[]=  "
{
                'title'         : ".$vodAssetArray[$i]['title'].",
                'description'   : ".$vodAssetArray[$i]['description'].",
                'thumbnail'     : [".$vodAssetArray[$i]['thumbnail']."],
                'large'         : [".$vodAssetArray[$i]['large']."],
                'button_list'   :
                [
                    { 'title':'Demo', 'url' : 'http://mypd.front.net/', 'new_window' : true }
                ],
                'tags'          : [".$vodAssetArray[$i]['genre']."]
 }
";
}
?>
<script type="text/javascript">
    $(function(){
        $("#demo").grid({
            'genre' : 'All',
            'items' :
            [
              <?php echo implode(',', $javascript); ?>
            ]
        });
    });
</script>

如果 JavaScript 在你的 PHP 文件中,你可以简单地回显 PHP 变量。例如,如果您的 PHP 数组$array:

'title'         : "<?php echo $array['title']; ?>",