The $group variable contains an array in which to output the last line without a comma. I do it like this:

 $group = '{"title":"Title", "artist":"Artist", "cover":"img/1.png", "file":"./file/1.mp3"},'; $numItems = count($group); $i=0; foreach($group as $filter) { if(++$i === $numItems) echo $group; else echo $group','; } 

does not work. How correct?

  • comparison $numItems-1 do - Lex Hobbit
  • From the point of view of performance, which, as a rule, does not matter in this case, this is done by inferring the first value, and then all subsequent ones, separated by commas before them. - user239133

9 answers 9

 $i = ''; foreach($group as $filter) { echo $i . $filter; $i = ','; } 

Although your $ group does not look like an array.

    The option of combining array elements into a string is, in my opinion, the most convenient.

     echo join(',', $group); 

      $ group is a string, not an array. Check, maybe you made a mistake?

      I suspect that $ group is a json object, then it can be converted into an array, and from there into a string:

       $group = '{"title":"Title", "artist":"Artist", "cover":"img/1.png", "file":"./file/1.mp3"}'; $array = json_decode($group, true); $result = []; foreach($array as $key => $value) { $result[] = $key . ':' . $value; } $result = implode(',', $result); 

      At the output we get:

      title: Title, artist: artist, cover: img / 1.png, file: ./ file / 1.mp3

      • $array = json_decode($group, true); $result = [];foreach($array as $key => $value) { $result[] = '"'.$key.'"' . ':' . '"'.$value.'"'; } $result = implode(', ', $result); echo '{'.$result. '},'; At the output, all with commas $ result = $ group. Need the last line without a comma - Sergey_777
      • Well, remove the comma here '{'. $ Result. '},'; - here at the end comma for what? Here is the output without a comma: ideone.com/9yrpIC - izmail

      You get JSON. Perhaps you should not transfer the extra comma when sending, or simply delete the last character.

       <?php $group = '{"title":"Title", "artist":"Artist", "cover":"img/1.png", "file":"./file/1.mp3"},'; $group_cut = substr($group, 0, -1); $arr = json_decode($group_cut, true); echo $arr['file']; ?> 

        You can make it even easier. Just replace

        '},'

        on

        '}'

         $group = '{"title":"Title", "artist":"Artist", "cover":"img/1.png", "file":"./file/1.mp3"},'; $group = str_replace('},','}',$group); 

          In the foreach condition, add as $key => $filter and add a condition inside the loop:

           if ( $key != ($numItems - 1) ) { echo $group . ','; } else { echo $group; } 

          And in the definition of the array to remove a comma at the end, that is, not:

           $group = '{"title":"Title", "artist":"Artist", "cover":"img/1.png", "file":"./file/1.mp3"},'; 

          And so:

           $group = '{"title":"Title", "artist":"Artist", "cover":"img/1.png", "file":"./file/1.mp3"}'; 

            In principle, they answered you, I’ll add a regular-order method from myself, let's say that you may have more than one array.

             $group = '{"title":"Title", "artist":"Artist", "cover":"img/1.png", "file":"./file/1.mp3"},'; $group = preg_replace('#\,{1}\z#', '', $group); $group_ARR = json_decode($group, true); var_dump($group); print_r($group_ARR); echo end($group_ARR); 
               <?php $group = '{"title":"Title", "artist":"Artist", "cover":"img/1.png", "file":"./file/1.mp3"},'; $group = substr($group, 0,-1); $array = json_decode($group); $result = []; foreach($array as $key => $value) { $result[$key] = $value; } echo "<pre>", var_dump($result); ?> 

                First: http://php.net/manual/ru/function.implode.php

                Then:

                http://php.net/manual/ru/function.rtrim.php

                The simplest explanation of the solution for your problem.