user3027770
user3027770

Reputation: 1

Show on last array

I have this code:

for($i=1;$i<=date("j");$i++) {
  $DataGraphLinesAff .= ($i == 1 ? '['.$i.', '.$InfosMembre['imp_'.$i.''].'],
  ' : '['.$i.', '.$InfosMembre['imp_'.$i.''].'],
  ');
}

which displays:

    [1, 0],
    [2, 0],
    [3, 0],
    [4, 0],
    [5, 0],
    [6, 0],
    [7, 0],
    [8, 0],
    [9, 0],
    [10, 0],
    [11, 0],
    [12, 0],
    [13, 34],

how change code to display last array without ,

How to display ['8', 0], ?

Upvotes: 0

Views: 41

Answers (4)

Krish R
Krish R

Reputation: 22741

You can substr to remove last character from string,

for($i=1;$i<=date("j");$i++) {
    $DataGraphLinesAff .= ($i == 1 ? '['.$i.', '.$InfosMembre['imp_'.$i.''].'],
    ' : '['.$i.', '.$InfosMembre['imp_'.$i.''].'],
    ');
}

 $DataGraphLinesAff =  substr(trim($DataGraphLinesAff), 0, -1);

Upvotes: 0

AbraCadaver
AbraCadaver

Reputation: 79024

You can use an array:

for($i=1;$i<=date("j");$i++) {
        $DataGraphLinesAff[] = '['.$i.', '.$InfosMembre['imp_'.$i.''].']';
}
$DataGraphLinesAff = implode(",\n", $DataGraphLinesAff);

Upvotes: 1

Aris
Aris

Reputation: 5055

you can trim the last comma from your string:

rtrim($DataGraphLinesAff, ",")

http://php.net/rtrim

Upvotes: 0

bredikhin
bredikhin

Reputation: 9045

for($i=1;$i<=date("j");$i++) {
        $DataGraphLinesAff .= ($i == 1 ? '['.$i.', '.$InfosMembre['imp_'.$i.''].'],
        ' : '['.$i.', '.$InfosMembre['imp_'.$i.''].']'.(($i==date("j"))?'':',
        '));
    }

Upvotes: 0

Related Questions