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

6
  • 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: stackoverflow.com/questions/1756015/… Commented Feb 18, 2014 at 19:02
  • Should $ora be an array? If so then the assignment and echo statements are wrong. Commented Feb 18, 2014 at 19:02
  • you logic in the code is not clear. What are you trying to do ? Commented Feb 18, 2014 at 19:02
  • use de3.php.net/array_diff Commented Feb 18, 2014 at 19:03
  • print $p and show us here, that would be helpful Commented Feb 18, 2014 at 19:05

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.