Asked  2 Years ago    Answers:  5   Viewed   259 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?

 Answers

5

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.

Solution:

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);
Tuesday, September 20, 2022
4

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

<?php
...
$new_array = [];
foreach ($my_array as $key => $value)
     $new_array[$key] = is_array($value) ? implode(",", $value) : $value;
Saturday, September 17, 2022
4

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')));
printArray($array);

function printArray($array)
{
    foreach($array as $key => $value)
    {
        if(is_array($value))
        {
            echo $key . "<br/>";
            printArray($value);
            echo "<br/>";
        }
        else
        {
            echo $value . "<br/>";
        }
    }
}

Will print:

Soorten
inpandig

Voorzieningen
Voorziening
voorzien van verwarming
voorzien van elektra
voorzien van water
met elektrische deur
Thursday, October 6, 2022
5

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();
$objPHPExcel->getActiveSheet()->fromArray($dataArray,null,"A2");
Wednesday, August 17, 2022
 
2

The actual problem is you're not assigning the result of array_reverse to anything

// change this
array_reverse($respArr);

// to this
$respArr = array_reverse($respArr);
Friday, August 19, 2022
 
benvd
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 

Browse Other Code Languages