0

I am wondering how to:

  • Make comma-separated list
  • Strip last comma from list

Here's an array example:

Array
(
    [name] => Array
        (
            [0] => Some message to display1
        )
    [test] => Array
        (
            [0] => Some message to display2
        )
    [kudos] => Array
        (
            [0] => Some message to display3
        )

)

I want to display it like this:

Comma-List: Some message to display1, Some message to display2, Some message to display3

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Tom
  • 3,717
  • 5
  • 26
  • 28
  • 3
    To all the answers merely directing toward `implode()`, there's another layer to this -- notice that it is a 2D array. – Michael Berkowski Aug 02 '12 at 16:10
  • @MichaelBerkowski: Divide an conquer: [How to “flatten” a multi-dimensional array to simple one in PHP?](http://stackoverflow.com/questions/526556/how-to-flatten-a-multi-dimensional-array-to-simple-one-in-php) – hakre Dec 20 '12 at 18:21
  • @hakre Vague memories of this question and 3 or 4 immediate `implode()`'s before they were eventually edited or deleted. I think I've linked at least a handful against that "flatten" question this week already, so here's one more. – Michael Berkowski Dec 20 '12 at 18:53
  • Yes, just stumbled over while editing PHP5 tag, and wondered there was no dupe then seeing your comment ;) – hakre Dec 20 '12 at 18:55

7 Answers7

2

Loop through the arrays and implode() after you've got each array.

$bigArray = array();
foreach($firstArray as $secondArray){
    if(is_array($secondArray)){
        $bigArray = array_merge($bigArray, $secondArray);
    }
}
$commaList = implode(",", $bigArray);
SomeKittens
  • 38,868
  • 19
  • 114
  • 143
1

so, revising my answer to actually address your question, you could do it with nested foreach loops like this:

<?php

$a1 = array(
    'name' => array( 0 => 'Some message to display1'),
    'test' => array( 0 => 'Some message to display2'),
    'kudos' => array( 0 => 'Some message to display3'),
    );

$final = "";
foreach($a1 as $innerarray){
    foreach($innerarray as $message){
        $final .= $message.", ";
    }
}

echo substr($final,0,-2);
?>
1

You can use implode to join values and array_map to extract them:

// this should be your array
$youArray = array(); 
// return first elements
$values = array_map(function($item) { return $item[0]; }, $youArray); 
// echo joined values
echo implode(',', $values);
Zbigniew
  • 27,184
  • 6
  • 59
  • 66
0
$array = array("some text","other text");
$impl = implode(",", $array);
echo $impl;
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
0

implode works on 1D arrays, but you have a 2D array, this will take a bit more work.

You can use array_map to flatten the array

$flatArray = array_map(function($a){
    // I implode here just in case the arrays
    // have more than one element, if it's just one
    // you can return '$a[0];' instead
    return implode(',', $a);
}, $array);

echo implode(',', $flatArray);
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
0

This works for your array and should work for arrays that are n levels deep:

$array = array(
    'name' => array('Some message to display1'),
    'test' => array('Some message to display2'),
    'kudos' => array('Some message to display3')
);

$mystr =  multi_join($array);
while (substr($mystr, -1) == ',') {
    $mystr = substr($mystr, 0, -1);
}
echo $mystr;

function multi_join($value) {
    $string = '';

    if (is_array($value)) {
        foreach ($value as $el) {
            $string.= multi_join($el);
        }
    } else {
        $string.= $value.',';
    }

    return $string;
}
hellsgate
  • 5,905
  • 5
  • 32
  • 47
  • 1
    Instead of concating to a string (and having to deal with the trailing comma), push it to an array, then implode that. – gen_Eric Aug 02 '12 at 16:27
0

Simplest for your specific sample data would be to access the 0 column of data and implode that. (Demo)

echo implode(', ', array_column($array, 0));

Here are a couple of additional techniques to add to this heap...

  1. Reindex the first level because the splat operator doesn't like associative keys, then merge the unpacked subarrays, and implode.

  2. Use array_reduce() to iterate the first level and extract the first element from each subarray, delimiting as desired without the use of implode().

Codes: (Demo)

$array = [
    'name' => ['Some message to display1'],
    'test' => ['Some message to display2'],
    'kudos' => ['Some message to display3']
];

echo implode(', ', array_merge(...array_values($array)));

echo "\n---\n";

echo array_reduce($array, function($carry, $item) {return $carry .= ($carry ? ', ' : '') . $item[0]; }, '');

Output:

Some message to display1, Some message to display2, Some message to display3
---
Some message to display1, Some message to display2, Some message to display3

The first technique will accommodate multiple elements in the subarrays, the second will not acknowledge more than one "message" per subarray.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136