0
    $a="abc";
    $b=123;
    $c=1;

    for ($i=0;$i<3;$i++) {
        $str="'" . $a . "' , '" . $b . "', '" . $c . "', '1'";
        $sum .= "(" . $str . "),";
    }
    //output ('abc' , '123', '1', '1'),('abc' , '123', '1', '1'),('abc' , '123', '1', '1'),
    echo $sum;

Can anyone help me with the above codes? I want the $sum to output ('abc' , '123', '1', '1'), one time only. That I have to check the $str is repeated.

Can anyone help me or give me some hits? Thanks.

6
  • I don't get the loop if you want to output once. Do you want to check inside the loop whether the string is being repeated on each iteration? Commented Aug 27, 2014 at 17:51
  • Why you need for loop there?? Commented Aug 27, 2014 at 17:51
  • $a, $b, and $c is the values from database. Commented Aug 27, 2014 at 17:56
  • That's not an answer to the questions. Why do you have the loop? Commented Aug 27, 2014 at 17:57
  • Looks like you tried the question on a simplified version of your problem. The thing is that you made it so simple that the question doesn't make sense. As Barmar comments above, based on the information you posted the loop does not make any sense. Try to rewrite your question with something more similar to the actual thing you're doing. Commented Aug 27, 2014 at 18:03

2 Answers 2

1

This will check if the string is the same. if it is, we move on

<?php
    $a="abc";
    $b=123;
    $c=1;

    $tempStr =  '';
    $str = '';
    $sum = '';
    for ($i=0;$i<3;$i++) {
        $tempStr = "'" . $a . "' , '" . $b . "', '" . $c . "', '1'";
        if ($tempStr === $str) {
            continue;
        }//END IF
        $str="'" . $a . "' , '" . $b . "', '" . $c . "', '1'";
        $sum .= "(" . $str . "),";
    }
    //output ('abc' , '123', '1', '1')
    echo rtrim($sum, ',');
?>

EDIT: added a fiddle here

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

4 Comments

This seems to be the solution, if this is what the OP was looking for. Still do not understand the point of the loop though. +1
@eluong He is going to be looping over database values (potentially in array format?) and wants to remove duplicate information.
Thank you. I will try that. The for loop seems must, because I got the data from the database, and loop through that into string. And I don't want some repeated values.
If it is leaving some duplicate values, just change the === to a double equal ==. Shouldnt have to but just in case.
0

It's concatenating to $str for each iteration of the for block. You have to take away the for statement so it only executes once. Then you will get ('abc' , '123', '1', '1'), one time only.

If you want to check if it's being repeated, you can parse by "," by using explode() http://www.w3schools.com/php/func_string_explode.asp and check how many elements are in the array.

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.