0

I have few FOREACH loops in php;

$c1 = 1;
$c2 = 1;
$c3 = 1;

foreach ($someArray as $a){
    echo $a;
    if (sizeof($someArray != $c1){
        echo " / ";
    }
    $c1++;
}

foreach ($otherArray as $b){
    echo $b;
    if (sizeof($otherArray != $c2){
        echo ", ";
    }
    $c2++;
}

// etc.

This seems somehow stupid, of course =) Is there any way to avoid declaring variables with same values and to use them in many FOREACH loops? Thanks in advance for any help!

6
  • Are you referring to the $c counter variables? Commented Oct 8, 2009 at 21:22
  • 1
    Some indication as to what the purpose of the code is would be helpful. Commented Oct 8, 2009 at 21:22
  • 1
    Also, watch out for syntax and logic errors. if (sizeof($someArray) != $c1) { Commented Oct 8, 2009 at 21:30
  • Ah yes, sorry for the typo... =( The purpose of code is just some general displaying of a multidimensional array from mysql query... Commented Oct 8, 2009 at 21:50
  • You should accept answers to your original questions, and make a new question when you have one. Commented Oct 8, 2009 at 22:10

1 Answer 1

7

It appears you are trying to do what the following code does much better:

$line = implode(' / ', $someArray);
echo $line;

$line = implode(', ', $otherArray);
echo $line;
Sign up to request clarification or add additional context in comments.

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.