How to remove the last character, store the result in a variable and use the variable outside of foreach?

Asked

Viewed 933 times

3

I have a code PHP thus:

foreach ($rows as $obj) :

    $all_ids = $obj->ID . ', ';

endforeach;

.. whose exit is 125, 148, 157, 169, 185,

How to remove the last comma and store the rest of the output in a variable to use it outside the foreach?

3 answers

3

I usually use a simpler approach when I want to turn an array or object into a comma-separated string.

<?php
   $rows = array("125", "148", "157", "169", "185");
   echo implode(',',$rows); //125,148,157,169,185
?>
  • 2

    obviously the best option for the question posed.

2

Although the @rray answer is quite simple and useful, there is also another way to get the desired result. Use a bool to know if an item is first or not on the list.

An example:

<?php
   $rows = array("125", "148", "157", "169", "185");
   $all_ids = "";
   $first_row = true;
   foreach ($rows as $item){
      if ( $first_row ) {
       $first_row = false;
       $all_ids .= $item;
      } else {
       $all_ids .= ", ".$item;
      }
   }

This is quite useful in languages that do not have a function trim() that accepts values beyond space.

  • If you can’t use the trim() it is very useful this code, also possible to do with regex.

1


You can use the Trim to remove the last character right, the second argument says which character should be.

Trim

<?php
   $rows = array("125", "148", "157", "169", "185");
   $all_ids = "";
   foreach ($rows as $item){
      $all_ids .= $item.", ";
   }

   echo trim(trim($all_ids),',');

Example - ideone


replace

Or with substr, which will remove the space and comma.

<?php
   $rows = array("125", "148", "157", "169", "185");
   $all_ids = "";
   foreach ($rows as $item){
      $all_ids .= $item.", ";
   }

   $all_ids = substr($all_ids, -0, -2);
   echo $all_ids;

Example - ideone


array_map

From php5.3 it is possible to use anonymous functions, which combined with array_map() eliminates the foreach. array_map apply a function to all elements of an array($row), anonymous function only returns property ID of the object, then just use the implode() to convert the array into a comma-separated string, as demonstrated by Jefferson Silva.

This approach was taken from PHP - Extracting a Property from an array of Objects

<?php
   //Monta um array igual ao da pergunta
   $valores = array("125", "148", "157", "169", "185");
   for($i=0; $i<5; $i++){
     $obj = new stdClass();
     $obj->ID = $valores[$i];
     $rows[] = $obj;
   }

   $all_ids  =  array_map(function($item){ return $item->ID; }, $rows);
   echo implode(',', $all_ids);

Example - ideone

  • As simple as it sounds, it didn’t work. You noticed that this string is the result of a foreach?

  • 1

    @Marcosvinicius, is he overwriting the value? gets only last is that? in this case change the = for .= that will concatenate the string.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.