0

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

ulissess
  • 23
  • 1
  • 4
  • 1
    this if ($p[$i] != $p[$i++]) will be true, it is the same is $p[$i] != $p[$i] because of the ++ placement. Read this: http://stackoverflow.com/questions/1756015/whats-the-difference-between-i-and-i-in-php – Michael Arenzon Feb 18 '14 at 19:02
  • Should `$ora` be an array? If so then the assignment and echo statements are wrong. – Dharman Feb 18 '14 at 19:02
  • you logic in the code is not clear. What are you trying to do ? – Hassan Feb 18 '14 at 19:02
  • use http://de3.php.net/array_diff –  Feb 18 '14 at 19:03
  • print $p and show us here, that would be helpful – Pranay Bhardwaj Feb 18 '14 at 19:05
  • $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 ----------------- – ulissess Feb 19 '14 at 01:36

2 Answers2

1

I'm not sure I understand your question.

If you want to check that every character in $p is the same you can do this:

for ($i = 0; $i < $count-1; $i++) {
    if ($p[$i] != $p[$i+1]){
        $ora = $p;
    }
    echo $ora."     "; 
}

Notice how $count should really be $count-1. Also make sure that $count=count($p) is $count=strlen($p)

Of course only if I understood correctly what you want to do.

EDIT: I automatically used $i+1 instead of $i++ but as the previous reply correctly states: Don't increment $i here! It will be a post increment anyway and thus absolutely unnecessary

emazzotta
  • 1,879
  • 3
  • 20
  • 31
0

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';

Community
  • 1
  • 1
Michael Arenzon
  • 541
  • 8
  • 16