0

Hi. I want to remove lines with same text. i wrote this code, but it doesnt work. where is the problem ?

$total=$_GET['name'];
$keyarr=explode("\n",$total);
$int=sizeof($keyarr);
  for($a=0;$a<$keyarr;$a++){
    for($b=$a+1;$b<5\$keyarr;$b++){
        if($keyarr[$a]==$keyarr[$b]){
            unset($keyarr[$b]);
        }
    }
 }
  echo $keyarr[$int-1];
 foreach($keyarr as $new){
echo $new."<br>";
}
3
Contributors
15
Replies
63
Views
4 Years
Discussion Span
Last Post by nstrazimiri
Featured Replies
  • Use array_unique() This might work $total=$_GET['name']; $keyarr=explode("\n",$total); // In order to process this array values here is the code $int=sizeof($keyarr); for($a=0;$a<$int;$a++){ for($b=$a+1;$b<$int;$b++){ if($keyarr[$a]==$keyarr[$b]){ unset($keyarr[$b]); } } } echo $keyarr[$int-1]; $keyarr = array_unique($keyarr, SORT_REGULAR); foreach($keyarr as $new){ echo $new."<br>"; } Read More

  • Okay? which one is line 7 in your file? It is most likely because you are trying to access a key in the array that doesn't exist. Read More

  • Wrong, I typed this line $keyarr = array_unique($keyarr, SORT_REGULAR); Any how, you are checking two array values to see if they match. One or both of the keys do not exist. You would need to check if they exist before you can compare them. $total=$_GET['name']; $keyarr=explode("\n",$total); // In order to … Read More

  • I couldn't see a problem so I ran your code and I got the same problem. The problem is the explode. Your exploding on newline but not everything has "\n" after it.. It works perfect if I change the explode to spaces and then change the string to be space … Read More

  • OR.. you could do something like this $total = str_replace("\n", ",", $total); $total = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $total); $keyarr=explode(",",$total); Read More

0

Hi. I want to remove lines with same text. i wrote this code, but it doesnt work. where is the problem ?

$total=$_GET['name'];
 $keyarr=explode("\n",$total);
// In order to  process this  array  values  here is the code
$int=sizeof($keyarr);
  for($a=0;$a<$int;$a++){
    for($b=$a+1;$b<$int;$b++){
        if($keyarr[$a]==$keyarr[$b]){
            unset($keyarr[$b]);
        }
    }
 }
  echo $keyarr[$int-1];
 foreach($keyarr as $new){
echo $new."<br>";
}
2

Use array_unique()

This might work

$total=$_GET['name'];
 $keyarr=explode("\n",$total);
// In order to  process this  array  values  here is the code
$int=sizeof($keyarr);
  for($a=0;$a<$int;$a++){
    for($b=$a+1;$b<$int;$b++){
        if($keyarr[$a]==$keyarr[$b]){
            unset($keyarr[$b]);
        }
    }
 }
  echo $keyarr[$int-1];
  $keyarr = array_unique($keyarr, SORT_REGULAR);
 foreach($keyarr as $new){
    echo $new."<br>";
}
0

it doesn't work yet.it comes with this error:
Notice: Undefined offset: 2 in C:\wamp\www\test.php on line 7

2

Okay? which one is line 7 in your file?

It is most likely because you are trying to access a key in the array that doesn't exist.

Edited by pixelsoul

2

Wrong, I typed this line

$keyarr = array_unique($keyarr, SORT_REGULAR);

Any how, you are checking two array values to see if they match. One or both of the keys do not exist. You would need to check if they exist before you can compare them.

$total=$_GET['name'];
$keyarr=explode("\n",$total);
// In order to  process this  array  values  here is the code
$int=sizeof($keyarr);
for($a=0;$a<$int;$a++){
    for($b=$a+1;$b<$int;$b++){
        if(isset($keyarr[$a]) && isset($keyarr[$b])){
            if($keyarr[$a]==$keyarr[$b]){
                unset($keyarr[$b]);
            }
        }
        else
        {
            echo "Does not exist!";
        }
    }
}
echo $keyarr[$int-1];
$keyarr = array_unique($keyarr, SORT_REGULAR);
foreach($keyarr as $new){
    echo $new."<br>";
}
0

thankyou man for the answering. now if i type:
aa
aa
bb
bb
is displays:
aa
bb
bb
but if i type:
aa
bb
aa
bb
cc
it displays:
aa
bb
wich means: if the last line is same as another line, it displays them both.
this is the only problem now :(

2

I couldn't see a problem so I ran your code and I got the same problem. The problem is the explode. Your exploding on newline but not everything has "\n" after it.. It works perfect if I change the explode to spaces and then change the string to be space delimited. Are you able to control the format of the string? Basically you need to make sure each value has a "\n" after it.

2

OR.. you could do something like this

$total = str_replace("\n", ",", $total);
$total = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $total);  

$keyarr=explode(",",$total);
1

Try this code, it worked for me ;)

<?php
$total=
"AA AA
AA AA
BB BB
BB BB
BB BB
CC CC
BB BB
BB BB
AA AA
CC CC
DD DD
DD DD"
;
$keyarr=explode("\n",$total);
$int=sizeof($keyarr);

$i=0;
$tmp = array();
$cur = 0;
$found = false;
while($i<$int){
    $found = false;
    for($j=0; $j<sizeof($tmp);$j++){
        if(trim(preg_replace('/\s\s+/', ' ', $keyarr[$i])) == trim(preg_replace('/\s\s+/', ' ', $tmp[$j]))){
            $found = true;
        }
    }

    if(!$found){
        $tmp[$cur++] = $keyarr[$i];
    }

    $i++;
}

foreach($tmp as $element){
    echo $element."<br>";
}
?>
0

same problem continues. if the last lines are same with each other, it displays them both

2

I explained above why you were seeing that happen. If you doing something like this below it will strip out all non-printable characters and replace them with a comma, and then you can split the string on commas. I tested this and there was no problem at that point.

$total = str_replace("\n", ",", $total);
$total = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $total);  
$keyarr=explode(",",$total);
2

Here is the full code I tested with

<?php

//$total=$_GET['name'];

$total = "aa
aa
bb
bb
cc
cc";

$total = str_replace("\n", ",", $total);
$total = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $total);  

$keyarr=explode(",",$total);

//echo $keyarr[$int-1];
$newArray = array_unique($keyarr);

foreach($newArray as $new){
    echo $new."<br>";
}
?>
0

i tried this code, and if the $total is like you gived it, its ok, it works.
but i am using a text area and $total=$_GET['name']; gets the string from that. and the problem continues ...

0

thx guys for attending into this. i solve it. thx cmps for you idea, it was great ^_^

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.