I am trying to generate a string from an array. Need to concatenate the array values with a small string AFTER the value. It doesn't work for the last value.

$data = array (
  1 => array (
    'symbol' => 'salad'
  ),
  2 => array (
    'symbol' => 'wine' 
  ),
  3 => array (
    'symbol' => 'beer'
  )
);

$symbols = array_column($data, 'symbol');
$string_from_array = join($symbols, 'bar');
echo($string_from_array);

// expected output: saladbar, winebar, beerbar
// output: saladbar, winebar, beer

6 Answers

3
Qirel On

You can achieve it a few different ways. One is actually by using implode(). If there is at least one element, we can just implode by the delimiter "bar, " and append a bar after. We do the check for count() to prevent printing bar if there are no results in the $symbols array.

$symbols = array_column($data, "symbol");
if (count($symbols)) {
    echo implode("bar, ", $symbols)."bar";
}
0
Community On

Try this:

$symbols = array_column($data, 'symbol');
foreach ($symbols as $symbol) {
    $symbol = $symbol."bar";
    echo $symbol;
    }

btw, you can't expect implode to do what you expect, because it places "bar" between the strings, and there is no between after the last string you get from your array. ;)

3
slevy1 On

You can also achieve the desired result using array_map(), as follows:

 <?php
$data = array (
  1 => array (
    'symbol' => 'salad'
  ),
  2 => array (
    'symbol' => 'wine' 
  ),
  3 => array (
    'symbol' => 'beer'
  )
);

$symbols = array_column($data, 'symbol');
$mapped = array_map(function($e){
            return $e . "bar";
},$symbols);
$string_from_array = join(", ",$mapped);
echo($string_from_array);

See live code

Array_map() takes every element of the $symbols array and appends the string "bar" with a callback, consisting of an anonymous function, which returns that result assigning it to a corresponding element in a new array $mapped. Then the array $mapped has the values of its elements joined with ", " to form the expected output which is then assigned to $string_from_array.

1
Kurohige On

The join() function is an alias of implode() which

Returns a string containing a string representation of all the array elements in the same order, with the glue string between each element.

So you need to add the last one by yourself

$data = array (
  1 => array (
    'symbol' => 'salad'
  ),
  2 => array (
    'symbol' => 'wine' 
  ),
  3 => array (
    'symbol' => 'beer'
  )
);

$symbols = array_column($data, 'symbol');
$string_from_array = join($symbols, 'bar');
if(strlen($string_from_array)>0)
     $string_from_array .= "bar";
echo($string_from_array);
1
Rakesh Kumar Jakhar On

You can use array_column and implode

$data = array (
  1 => array (
   'symbol' => 'salad'
  ),
  2 => array (
    'symbol' => 'wine' 
  ),
  3 => array (
   'symbol' => 'beer'
  )
 );
 $res  = implode("bar,", array_column($data, 'symbol'))."bar";

Live Demo

0
The fourth bird On

Another way could be using a for loop:

$res = "";
$count = count($data);
for($i = 1; $i <= $count; $i++) {
    $res .= $data[$i]["symbol"] . "bar" . ($i !== $count ? ", " : "");
}
echo $res; //saladbar, winebar, beerbar

Php demo