Php 传递数组值时遇到问题

Php 传递数组值时遇到问题,php,Php,我正在构建一个PHP程序,基本上只从我的twitter提要中获取图像链接,并将它们显示在页面上。我已经设置了3个组件,它们都可以自己正常工作 第一个组件是TwitterOAuth组件,它获取tweet文本并创建一个数组,这本身就可以很好地工作 第二个是一个函数,它处理tweet并只返回包含图像链接的tweet,这也可以正常工作 程序在第三部分中出现故障,当链接被处理并显示图像时,我自己运行该程序没有问题,从我的故障排除尝试来看,它似乎在$images()处出现故障;数组,因为该数组为空 我肯定我

我正在构建一个PHP程序,基本上只从我的twitter提要中获取图像链接,并将它们显示在页面上。我已经设置了3个组件,它们都可以自己正常工作

第一个组件是TwitterOAuth组件,它获取tweet文本并创建一个数组,这本身就可以很好地工作

第二个是一个函数,它处理tweet并只返回包含图像链接的tweet,这也可以正常工作

程序在第三部分中出现故障,当链接被处理并显示图像时,我自己运行该程序没有问题,从我的故障排除尝试来看,它似乎在$images()处出现故障;数组,因为该数组为空

我肯定我犯了一个愚蠢的错误,但我已经试着找到它一天多了,似乎无法修复它。任何帮助都会很好!谢谢大家

代码:

<?php

if ($result['socialorigin']== "twitter"){
$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
$token = $twitterObj->getAccessToken();
$twitterObj->setToken($result['oauthtoken'], $result['oauthsecret']);
$tweets = $twitterObj->get('/statuses/home_timeline.json',array('count'=>'200'));

         $all_tweets = array();
         $hosts  = "lockerz|yfrog|twitpic|tumblr|mypict|ow.ly|instagr";



         foreach($tweets as $tweet) {

         $twtext = $tweet->text;

         if(preg_match("~http://($hosts)~", $twtext)){

         preg_match_all("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", $twtext, $matches, PREG_PATTERN_ORDER);

         foreach($matches[0] as $key2 => $link){

         array_push($all_tweets,"$link");

         }

         }

         }
function height_compare($a1, $b1)
{
    if ($a1 == $b1) {
        return 0;
    }
    return ($a1 > $b1) ? -1 : 1;
}

foreach($all_tweets as $alltweet => $tlink){
$doc = new DOMDocument();
// Okay this is HTML is kind of screwy
// So we're going to supress errors
@$doc->loadHTMLFile($tlink);

// Get all images
$images_list = $doc->getElementsByTagName('img');

$images = array();
foreach($images_list as $image) {

  // Get the src attribute
  $image_source = $image->getAttribute('src');

  if (substr($image_source,0,7)=="http://"){
  $image_size_info = getimagesize($image_source);

  $images[$image_source] = $image_size_info[1];
  }
}



// Do a numeric sort on the height
uasort($images, "height_compare");
$tallest_image = array_slice($images, 0,1);
$mainimg = key($tallest_image);

echo "<img src='$mainimg' />";


 }
 print_r($all_tweets);
 print_r($images);

}

更改获取实际图像的for循环,以将
图像
数组移到for循环之外。这将防止循环每次通过时清除它

  $images = array();
foreach($all_tweets as $alltweet => $tlink){
  $doc = new DOMDocument();
  // Okay this is HTML is kind of screwy
  // So we're going to supress errors
  @$doc->loadHTMLFile($tlink);

  // Get all images
  $images_list = $doc->getElementsByTagName('img');    
  foreach($images_list as $image) {

    // Get the src attribute
    $image_source = $image->getAttribute('src');

    if (substr($image_source,0,7)=="http://"){
      $image_size_info = getimagesize($image_source);

      $images[$image_source] = $image_size_info[1];
    }
  }

  // Do a numeric sort on the height
  uasort($images, "height_compare");
  $tallest_image = array_slice($images, 0,1);
  $mainimg = key($tallest_image);

  echo "<img src='$mainimg' />";
}
$images=array();
foreach($alltweet=>$tlink){
$doc=新的DOMDocument();
//好吧,这是HTML有点疯狂
//所以我们要消除错误
@$doc->loadHTMLFile($tlink);
//获取所有图像
$images\u list=$doc->getElementsByTagName('img');
foreach($images\u列表为$image){
//获取src属性
$image_source=$image->getAttribute('src');
if(substr($image_source,0,7)==“http://”){
$image\u size\u info=getimagesize($image\u source);
$images[$image\u source]=$image\u size\u info[1];
}
}
//对高度进行数字排序
uasort($images,“height_compare”);
$highter_image=数组_切片($images,0,1);
$mainimg=键($u图像);
回声“;
}

子字符串比较有用吗?程序中的每个元素都能独立工作,只有当我把它们放在一起时,子字符串比较才失效,因为出于某种原因,$images()数组并没有奇怪地积累任何链接。不幸的是,我将数组移到了foreach循环之上,仍然并没有运气,当我打印数组时,它仍然返回空的,我不明白为什么会发生这种情况,如果我只使用数组或链接运行代码的那一部分,它工作得很好,但是与其他部分结合使用,它就会完全消失