我如何获得数组结果的值?

时间:2017-07-25 06:08:22

标签: javascript php arrays

我是新手,我在php工作。我有一个问题,我有这个来自php脚本的“数组”结果(这个数组是用html页面的php提取的):

Array ( [0] => {"autostart": false,"controls": true,"flashplayer": "/jwplayer7/jwplayer.flash.swf","image": "I NEED THIS", ga: {}, "mute": false, "ph": 1, "preload": "none", "primary": "html", "repeat": false, "skin": { "name": "tube" }, "stagevideo": true, "stretching": "uniform", "visualplaylist": true, "width": "100%", "aspectratio": "16:9", "provider": 'http', "startparam": "start", tracks: [{"file":"I WANT THIS","kind":"thumbnails"}], "sources": [{"file":"I NEED THIS","label":"480p"},{"file":"I NEED THIS","label":"720p"},"I WANT THIS"] ,"logo": {"logoBar": "I NEED THIS", "target": "blank","link": ""},"displaydescription": false,"displaytitle": false , "abouttext": "RapTu Player", "aboutlink": "" } )

我只需要image:sources:[{"file";}}中的值,但我无法获取值,没有,我尝试使用javascript:

var str = '<?php echo $jw; ?>';
var json = JSON.parse(str);
var parse = $.parseJSON(file);

$jw是数组的变量,我没有任何结果,没有打印任何内容。你可以帮帮我吗? (抱歉我的英文)。

升级:我使用以下方法解决了这个问题:

object = [<?php echo $jw; ?>]
for(f=0;f<object.length;f++){
}

谢谢大家!

2 个答案:

答案 0 :(得分:0)

如果你想这样做的话。 然后使用

var json = '<?php echo json_encode($jw); ?>';

并且在json变量中你有来自数组的json

或者如果您只是来源,则可以使用

var json = '<?php echo json_encode($jw[0]["sources"]); ?>';

但仅适用于数组$ jw

中的第一个元素

答案 1 :(得分:0)

在php中: - 在php中正确编码字符串

$json = json_encode('{"autostart": false,"controls": true,"flashplayer": "/jwplayer7/jwplayer.flash.swf","image": "I NEED THIS", ga: {}, "mute": false, "ph": 1, "preload": "none", "primary": "html", "repeat": false, "skin": { "name": "tube" }, "stagevideo": true, "stretching": "uniform", "visualplaylist": true, "width": "100%", "aspectratio": "16:9", "provider": "http", "startparam": "start", tracks: [{"file":"I WANT THIS","kind":"thumbnails"}], "sources": [{"file":"I NEED THIS","label":"480p"},{"file":"I NEED THIS","label":"720p"},"I WANT THIS"] ,"logo": {"logoBar": "I NEED THIS", "target": "blank","link": ""},"displaydescription": false,"displaytitle": false , "abouttext": "RapTu Player", "aboutlink": ""}');
$array1 = array($json);

将$ array1传递给脚本中的html: -

var str = '<?php echo $jw; ?>'; // echo $array1[0] as $jw
var json = JSON.parse(str);    
var parse = $.parseJSON(file);