【问题标题】:How I can get values of array result?如何获取数组结果的值?
【发布时间】:2017-07-25 06:08:22
【问题描述】:

我是新手,我在 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++){
}

谢谢大家!

【问题讨论】:

  • $jw is the variable of the array - 这个变量在哪里被赋值?
  • $jw 的值为Array ( [0] =&gt; {"autostart": false....
  • 图像是您需要从该 json 获得的唯一值吗?
  • 没关系,我现在看到字符串中有更多I need this
  • @Andreas 是的!我需要“图像”和“文件”:)

标签: javascript php arrays


【解决方案1】:

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

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

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

或者,如果您只有可以使用的来源

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

但仅适用于数组 $jw 中的第一个元素

【讨论】:

  • 我尝试使用此代码 &lt;script type="text/javascript"&gt; var str = '&lt;?php echo json_encode($jw); ?&gt;'; var json = JSON.parse(str); var parse = $.parseJSON(autostart); document.writeln("&lt;br&gt;autostart:"+json.image); &lt;/script&gt; 但我没有结果
  • @R.13 向我们展示一下 PHP 脚本生成的实际代码怎么样?告诉你出了什么问题会容易得多。 var str = ???
  • 如果你有数组 0 json 你只有 $jw[0]
  • @smajl 嗨!我在 php 中有这个:第一:我得到了 html 页面的内容 - $data = file_get_contents($url); 第二:我找到了脚本 - preg_match_all("/&lt;script(.*)&gt;([^]*?)/",$data, $matches);` 第三:我得到$matches - $varmatch = $matches[1][8]; 第四:我得到$matches - preg_match_all('#\((.*?)\)#', $varmatch, $vid); 的内容第五:我得到新的匹配 - js = $vid[0][1]; 第六:我再次得到$vid - preg_match_all('#\((.*?)\)#', $js, $vars); Sevent 的内容:我有数组结果 - $jw = $vars[1][0]; :)
  • preg_match_all 在这种情况下返回索引为 0 的数组。然后使用 var str = '';
【解决方案2】:

在 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); 

【讨论】:

  • 嗨!我试过这样:$json = json_encode('$jw'); $array1 = array($json) 在 javascript 中:&lt;script type="text/javascript"&gt; var str = '&lt;?php echo $jw; ?&gt;'; var json = JSON.parse(str); var parse = $.parseJSON(file); document.write("&lt;br&gt;file:"+parse); &lt;/script&gt; 没有任何反应,我做错了什么?
  • @R.13 你用php正确编码了你的字符串吗?使用 json_encode() 函数
  • 是的,我想,我现在尝试了:$jw = $vars[1][0]; -> $arr = array($jw); -> $jw1 = json_encode($arr); -> &lt;script type="text/javascript"&gt; var str = '&lt;?php echo $jw1; ?&gt;'; var json = JSON.parse(str); var parse = $.parseJSON(autostart\); document.write("&lt;br&gt;file:"+parse); &lt;/script&gt; 我有同样的问题,没有任何反应,如果我尝试使用var json '&lt;?php echo json_encode($jw[0]["sources"]); ?&gt;'; 结果是:警告:非法字符串偏移“来源”,有什么不好?
猜你喜欢
  • 1970-01-01
  • 2019-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-19
  • 1970-01-01
  • 2022-10-26
  • 1970-01-01
相关资源
最近更新 更多