Asked  7 Months ago    Answers:  5   Viewed   41 times

In terms of my data structure, I have an array of communications, with each communications_id itself containing three pieces of information: id, score, and content.

I want to implode this array in order to get a comma separated list of ids, how do I do this?



Update for PHP 5.5

PHP 5.5 introduces array_column which is a convenient shortcut to a whole class of array_map usage; it is also applicable here.

$ids = array_column($communications, 'id');
$output = implode(',', $ids);

Original answer

You need to make an array of just ids out of your array of communications. Then the implode would be trivial.

Hint: the function for that is array_map.


Assumes PHP 5.3, otherwise you 'd have to write the callback as a string.

$ids = array_map(function($item) { return $item['id']; }, $communications);
$output = implode(',', $ids);
Wednesday, March 31, 2021
answered 7 Months ago

Just create a new array and set the elements (-;

$new_array = [];
foreach ($my_array as $key => $value)
     $new_array[$key] = is_array($value) ? implode(",", $value) : $value;
Wednesday, March 31, 2021
answered 7 Months ago

You should modify your call for array_unique to have it include the SORT_REGULAR flag.

$arr2 = array_unique($arr, SORT_REGULAR);
Wednesday, March 31, 2021
answered 7 Months ago

A recursive function would do the trick:

$array = array(
    'Soorten' => array('Soort' => 'inpandig'), 
    'Voorzieningen' => array('Voorziening' => array('voorzien van verwarming', 'voorzien van elektra', 'voorzien van water', 'met elektrische deur')));

function printArray($array)
    foreach($array as $key => $value)
            echo $key . "<br/>";
            echo "<br/>";
            echo $value . "<br/>";

Will print:


voorzien van verwarming
voorzien van elektra
voorzien van water
met elektrische deur
Wednesday, March 31, 2021
answered 7 Months ago

When you set cell values individually, you have the option of setting the datatype explicitly, but when you use the fromArray() method, you don't have this option.

However, by default, PHP uses a default value binder to identify datatypes from the values passed, and set the cell datatype accordingly. This default behaviour is defined in a class /PHPExcel/Cell/DefaultValueBinder.php.

So you can create your own value binder, as described in the PHPExcel Documentation, that would set every value as a string datatype.

Something like:

class PHPExcel_Cell_MyColumnValueBinder extends PHPExcel_Cell_DefaultValueBinder implements PHPExcel_Cell_IValueBinder
    protected $stringColumns = [];

    public function __construct(array $stringColumnList = []) {
        // Accept a list of columns that will always be set as strings
        $this->stringColumns = $stringColumnList;

    public function bindValue(PHPExcel_Cell $cell, $value = null)
        // If the cell is one of our columns to set as a string...
        if (in_array($cell->getColumn(), $this->stringColumns)) {
            // ... then we cast it to a string and explicitly set it as a string
            $cell->setValueExplicit((string) $value, PHPExcel_Cell_DataType::TYPE_STRING);
            return true;
        // Otherwise, use the default behaviour
        return parent::bindValue($cell, $value);

// Instantiate our custom binder, with a list of columns, and tell PHPExcel to use it
PHPExcel_Cell::setValueBinder(new PHPExcel_Cell_MyColumnValueBinder(['A', 'B', 'C', 'E', 'F']));

$objPHPExcel = new PHPExcel();
Friday, May 28, 2021
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :