Reputation: 23
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'??
11:00 PM
11:00 PM
11:00 PM
11:00 PM
11:00 PM
11:00 PM
11:00 PM
11:00 PM
Upvotes: 0
Views: 120
Reputation: 2039
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
Upvotes: 1
Reputation: 551
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';
Upvotes: 0