Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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
Php 如何在一个循环中使用两个对象键及其值_Php_Json_Facebook_Api_Loops - Fatal编程技术网

Php 如何在一个循环中使用两个对象键及其值

Php 如何在一个循环中使用两个对象键及其值,php,json,facebook,api,loops,Php,Json,Facebook,Api,Loops,我有一个JSON对象,它包含两个键events和feed,这两个键都包含来自facebook页面的值。数据需要显示在我的网站上混合通过彼此 像这样: Facebook post1 Facebook post2 Event1 Facebook post3 Event2 Facebook post4 问题是如何从一个foreach循环中的两个键调用值 我的对象看起来是什么样的一个小示例(可能有点输入错误,因为我无法粘贴整个对象,它有30000行长): 正如您所看到的,feed和events都包含我

我有一个JSON对象,它包含两个键
events
feed
,这两个键都包含来自facebook页面的值。数据需要显示在我的网站上混合通过彼此

像这样:

Facebook post1
Facebook post2
Event1
Facebook post3
Event2
Facebook post4
问题是如何从一个foreach循环中的两个键调用值

我的对象看起来是什么样的一个小示例(可能有点输入错误,因为我无法粘贴整个对象,它有30000行长):

正如您所看到的,
feed
events
都包含我想要显示的数据。我当前的php代码如下:

<?
    error_reporting(-1);
    $json_object = file_get_contents("https://graph.facebook.com/v2.10/165327986836920?fields=events%7Bname%2Cdescription%7D%2Cfeed%7Bfrom%2Cmessage%2Cfull_picture%7D&access_token=1234");


    $feedarray = json_decode($json_object);

  // echo '<pre>';
  // print_r($feedarray->events);
  // echo '</pre>';

    $f = 0;

    foreach ( $feedarray->events->data as $key => $feed_data )
    {

        if($feed_data->full_picture != ''){
            $fbimage = $feed_data->full_picture;
        }else{
            $fbimage = 'images/backgroundheader.jpg';
        }

        $shortstrfb = substr($feed_data->message, 0, 170) . '...';
        if($feed_data->message != ''){
            $f++;
        }

        if($f > 10){
            break;
        }

        echo $feed_data->from->name;



        if($feed_data->from->name == 'Rotterdam Ahoy'){
            $facebookfeed .= '
                    <div class="col-sm-6 col-lg-4 blog-isotope-item">
                        <div class="itemContainer zn_columns zn_columns3">
                            <div class="itemThumbnail">
                                <a href="'.$feed_data->permalink_url.'" target="_blank">
                                    <img width="760" height="508" src="'.$fbimage.'" class="wp-post-image" alt="'.$feed_data->name.'">
                                </a>

                                <div class="overlay">
                                    <div class="overlay__inner">
                                        <a href="'.$feed_data->permalink_url.'" target="_blank" class="readMore" title="" data-readmore="Bekijk op Facebook"></a>
                                    </div>
                                </div>
                            </div>

                            <div class="itemHeader">
                                <h3 class="itemTitle">
                                    <a href="'.$feed_data->permalink_url.'" target="_blank">'.$feed_data->name.'</a>
                                </h3>

                                <div class="post_details">
                                    <span class="catItemDateCreated"><span class="glyphicon glyphicon-calendar"></span> '.date("d-m-Y",strtotime($feed_data->updated_time)).'</span>
                                    <span class="catItemAuthor">by <a href="'.$feed_data->permalink_url.'" title="'.$feed_data->name.'" rel="author">'.$feed_data->name.'</a></span>
                                </div>
                            </div>

                            <div class="itemBody">
                                <div class="itemIntroText">
                                    '.$shortstrfb.'
                                </div>
                                <div class="clear">
                                </div>
                            </div>
                        </div>
                    </div>';
        }
    }
      if(isset($facebookfeed)){
        echo $facebookfeed;
      }
    ?>

如何将输出与第一个循环解耦。您可以循环建立一个新的位数组,按照您想要的顺序输出,然后循环该数组,写入关于将输出与第一个循环解耦的信息。您可以循环建立一个新的位数组,按照您想要的顺序输出所需的位,然后循环通过该数组写出信息
<?
    error_reporting(-1);
    $json_object = file_get_contents("https://graph.facebook.com/v2.10/165327986836920?fields=events%7Bname%2Cdescription%7D%2Cfeed%7Bfrom%2Cmessage%2Cfull_picture%7D&access_token=1234");


    $feedarray = json_decode($json_object);

  // echo '<pre>';
  // print_r($feedarray->events);
  // echo '</pre>';

    $f = 0;

    foreach ( $feedarray->events->data as $key => $feed_data )
    {

        if($feed_data->full_picture != ''){
            $fbimage = $feed_data->full_picture;
        }else{
            $fbimage = 'images/backgroundheader.jpg';
        }

        $shortstrfb = substr($feed_data->message, 0, 170) . '...';
        if($feed_data->message != ''){
            $f++;
        }

        if($f > 10){
            break;
        }

        echo $feed_data->from->name;



        if($feed_data->from->name == 'Rotterdam Ahoy'){
            $facebookfeed .= '
                    <div class="col-sm-6 col-lg-4 blog-isotope-item">
                        <div class="itemContainer zn_columns zn_columns3">
                            <div class="itemThumbnail">
                                <a href="'.$feed_data->permalink_url.'" target="_blank">
                                    <img width="760" height="508" src="'.$fbimage.'" class="wp-post-image" alt="'.$feed_data->name.'">
                                </a>

                                <div class="overlay">
                                    <div class="overlay__inner">
                                        <a href="'.$feed_data->permalink_url.'" target="_blank" class="readMore" title="" data-readmore="Bekijk op Facebook"></a>
                                    </div>
                                </div>
                            </div>

                            <div class="itemHeader">
                                <h3 class="itemTitle">
                                    <a href="'.$feed_data->permalink_url.'" target="_blank">'.$feed_data->name.'</a>
                                </h3>

                                <div class="post_details">
                                    <span class="catItemDateCreated"><span class="glyphicon glyphicon-calendar"></span> '.date("d-m-Y",strtotime($feed_data->updated_time)).'</span>
                                    <span class="catItemAuthor">by <a href="'.$feed_data->permalink_url.'" title="'.$feed_data->name.'" rel="author">'.$feed_data->name.'</a></span>
                                </div>
                            </div>

                            <div class="itemBody">
                                <div class="itemIntroText">
                                    '.$shortstrfb.'
                                </div>
                                <div class="clear">
                                </div>
                            </div>
                        </div>
                    </div>';
        }
    }
      if(isset($facebookfeed)){
        echo $facebookfeed;
      }
    ?>