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>";
}

Recommended Answers

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>";
}
Jump to Post

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.

Jump to Post

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 …
Jump to Post

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 …

Jump to Post

All 15 Replies

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>";
}

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>";
}

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

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.

line 7 of the file you typed.
7. if($keyarr[$a]==$keyarr[$b])

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>";
}

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 :(

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.

OR.. you could do something like this

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

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

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>";
}
?>

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

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

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>";
}
?>

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 ...

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.