douyouqian8550 2014-02-18 18:57
浏览 18

比较元素数组PHP

I've a problem with this code: In the array $p i have characters and i'd like to compare the elements of the array if i find different elements i save the information in an other array called $ora, but it isnt work, what's the problem?? Thank you very mych

$uri = 'http://www.site.com';
$output=file_get_contents($uri);

  if (preg_match_all('/<td colspan="1"><div class="tbl_EPG_TimesColumn.*?">(.*?)<\/div><\/td>/s', $output,  $posts, PREG_PATTERN_ORDER)){

 $p=$posts[0];

   }

$count=count($p);

  $ora = array();

  for ($i = 0; $i < $count; $i++) {

    if ($p[$i] != $p[$i++]){

     $ora =  $p;

   }

    echo $ora."     "; 

}

EDIT:

$count=count($p);

$ora = array();

for ($i = 0; $i < $count-1; $i++) {

 for ($j = 0; $j < $count-1; $j++) {

    if ($p[$i] != $p[$i+1]){

            $ora[$j] =  $p[$i];
     }  
  }

}

$count2=count($ora);

for ($j = 0; $j < $count2; $j++) {

echo $ora[$j]." ";

}


i tried it but it doesnt work, what is the wrong'??

this is the output:

11:00 PM

11:00 PM

11:00 PM

11:00 PM

11:00 PM

11:00 PM

11:00 PM

11:00 PM

11:00 PM

  • 写回答

2条回答 默认 最新

  • dongzhuohan7085 2014-02-18 19:06
    关注

    Well the if will never be true, as you are doing post-increment which means that this if: $p[$i] != $p[$i++] would be equal in meaning to $p[$i] != $p[$i]. You can read more about it here. Also its not that clear about what you are trying to do. If you want to remove duplicate values from your array, you should use array_unique.

    And be aware that $ora is array, and each time when the if will be true it'll overrite the previus value. The right way to do it is adding elements to $ora array like this: $ora[] = 'new value';

    评论

报告相同问题?