Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将代码从javascript重写为PHP_Javascript_Php_Xml - Fatal编程技术网

将代码从javascript重写为PHP

将代码从javascript重写为PHP,javascript,php,xml,Javascript,Php,Xml,我有javascript代码: if (imageData[imageCount].comments.data != null) { text = 'Comments Data:<br />'; imageData[imageCount].comments.data.forEach(function(comment){ text += comment.from.username + ': ' + comment.text + '<br />'; }); } fo

我有javascript代码:

if (imageData[imageCount].comments.data != null) 
{
 text = 'Comments Data:<br />';
 imageData[imageCount].comments.data.forEach(function(comment){
 text += comment.from.username + ': ' + comment.text + '<br />';
});
}
foreach($contents->data as$data){

foreach($data->comments->data as$comment){
if(preg#u match(“/#title/”,$comment->text)){
$komen=preg#u replace(“/#title/”,“$comment->text);
打破
}else{$komen='No Title';}
}
foreach($data->comments->data as$comment){
if(preg#u match(“/#price/”,$comment->text)){
$harga=preg#u replace(“/#price/”,“$comment->text);
打破
}else{$harga='0';}
}
回声'
低分辨率->url。“”width=“100%”/>
“.$data->caption->text”
]]>

您在最后一行忘记了$,一些括号设置不正确。要在PHP中使用concat字符串,请使用“.”而不是“+”

foreach($contents->data as$data){
如果($data->comments->data!=null){
foreach($data->comments->data as$comment){
$text=$comment->from->username.':'.$comment->text.
; } } }
comment
会引起问题。
$comment
,也许?当然,在PHP中,
是concatation操作符。你在做一堆“字符串数学”“使用
+
.PHP也不使用
+
进行连接。
$contents->data
$data->comments->data
数组吗?您没有指出是哪个foreach导致了问题。//初始化类$instagram=new instagram($accessToken);$instagram->setAccessToken($accessToken);$contents=$instagram->getUserMedia($userID,$settings['count']);我已经编辑了这个问题。请查看上面的内容,确保包含的数据是对象、数组还是字符串:)
foreach ($contents->data as $data) { 
if ($data->comments->data != null) 
{
 foreach($data->comments->data as $comment)
 {
  $text = comment->from->username + ': ' + comment->text + '<br />';
 });
}
<products>
<category categoryName="Instagram">
    <?php
foreach($data->comments->data as $comment){
          if(preg_match('/#title/', $comment->text)){
          $komen = preg_replace('/#title/', '', $comment->text);
          break;
          } else { $komen = 'No Title'; }
}

foreach($data->comments->data as $comment){
          if(preg_match('/#price/', $comment->text)){
          $harga = preg_replace('/#price/', '', $comment->text);
          break;
          } else { $harga = '0'; }
}


          echo '<product productName="'. $komen .'" productID="' . $data->id . '" thumbPath="' . $data->images->thumbnail->url . '" productPrice="'. $harga .'">
        <details>
                    <![CDATA[
                    <img src="' . $data->images->low_resolution->url . '" width="100%"/>
        ' . $data->caption->text . '
                    ]]>
        </details>
</product>
foreach ($contents->data as $data) {
    if ($data->comments->data != null) {
        foreach ($data->comments->data as $comment) {
            $text = $comment->from->username . ': ' . $comment->text . '<br />';
        }
    }
}