1

This is my part of code:

for( $i = $aKeys['iStart']; $i < $aKeys['iEnd']; $i++ )
{
        $aData = $this->aProducts[$aProducts[$i]];

        $content .= '"'.$aData['sName'].'"';

        if ($i < $aKeys['iEnd'])  
        {
           $content .= ', '; 
        } 
        $i2++;
} 

Full code gives me this as result:

["word1", "word2", "word3", ] 

This is a simple array, which I will use, but this won't work because after word3 there is a comma sign. How to write this if statement to get result like: ["word1", "word2", "word3"] ?

2
  • 1
    you can use array_push(); to eliminate the comma thingy :) Commented Mar 30, 2016 at 9:18
  • 1
    instead of appending to $content you can create array like $content = [] and use array_push($aData['sName'] and to get result like ["word1","word2","word3"] simply use json_encode($content); Commented Mar 30, 2016 at 9:20

5 Answers 5

5

Put a condition on the length-1 to fix your bug.

for( $i = $aKeys['iStart']; $i < $aKeys['iEnd']; $i++ ){
        $aData = $this->aProducts[$aProducts[$i]];

        $content .= '"'.$aData['sName'].'"';

        if ($i < $aKeys['iEnd']-1) {
        $content .= ', '; 
        } 
        $i2++;
      } 
Sign up to request clarification or add additional context in comments.

1 Comment

This is it! Thank you!
1

Alternatively you can use an array for this. And at the end simply implode them.

for( $i = $aKeys['iStart']; $i < $aKeys['iEnd']; $i++ ){
    $aData = $this->aProducts[$aProducts[$i]];
    $content[] = '"'.$aData['sName'].'"';
}

$content = '"' . implode('","', $content) . '"';

Comments

1

You can use rtrim() to remove comma.

rtrim($content, ',');

As I can see you want all names as comma separated. Than you can do like that as well :

$content = array();
for( $i = $aKeys['iStart']; $i < $aKeys['iEnd']; $i++ ){
    $aData = $this->aProducts[$aProducts[$i]];
    $content[] = $aData['sName'];
}

echo implode(',',$content);

2 Comments

please add more detail and maybe a link to the actual function page : php.net/manual/en/function.ltrim.php
Oops sorry we need to use rtrim. Thanks @Sougata
1
for( $i = $aKeys['iStart']; $i < $aKeys['iEnd']; $i++ ){
        $aData = $this->aProducts[$aProducts[$i]];

        $content .= '"'.$aData['sName'].'"';

        if ($i < $aKeys['iEnd'] && $i!=($aKeys['iEnd']-1)) { //this condition also considers $i not to be the last element of the array before appending the comma to it.
        $content .= ', '; 
        } 
        $i2++;
      } 

Comments

0

Use array_push(); function

$var = array();

loop(condition){
    array_push($var, $value);
}

3 Comments

OP wants a string with , separated values not an array.
@Sougata You should use Matt's solution and to get comma separated result you can use json_encode($array)
@akshaykhale, Yes maybe but OP wants manual , separation. It's okay ^_^

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.