Asked  7 Months ago    Answers:  5   Viewed   39 times

I am working with a one dimensional array in PHP. I would like to detect the presence of duplicate values, then count the number of duplicate values and out put the results. For example, given the following array:

$array = array('apple', 'orange', 'pear', 'banana', 'apple',
   'pear', 'kiwi', 'kiwi', 'kiwi');

I would like to print:

apple (2)
orange
pear (2)
banana
kiwi (3)

Any advice on how to approach this problem?

Thanks.

Mike

 Answers

34

You can use array_count_values function

$array = array('apple', 'orange', 'pear', 'banana', 'apple',
'pear', 'kiwi', 'kiwi', 'kiwi');

print_r(array_count_values($array));

will output

Array
(
   [apple] => 2
   [orange] => 1
   [pear] => 2
   etc...
)
Wednesday, March 31, 2021
 
rlanvin
answered 7 Months ago
30

Use array_unique().

Example:

$array = array(1, 2, 2, 3);
$array = array_unique($array); // Array is now (1, 2, 3)
Wednesday, March 31, 2021
 
pwaring
answered 7 Months ago
39

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");
Friday, May 28, 2021
 
Wilk
answered 5 Months ago
92

Function

function font_merge_unique($col, $arr, $exj)
{
    $test = array();

    foreach($arr AS $key => $row)
    {
        $test[$key] = strtolower(preg_replace('/[^A-Za-z0-9]/', '', $row[$col]));
        foreach($exj AS $index)
        {
            $arr[$key][$index] = array_map('trim', (is_array($arr[$key][$index]) ? $arr[$key][$index] : explode(',', $arr[$key][$index])));
        }
    }

    $unique = array_unique($test);

    $dupes = array_diff_key($test, $unique);

    $list = array();

    foreach($unique AS $key => $n)
    {
        $list[$key] = $arr[$key];
    }

    foreach($dupes AS $di => $row)
    {
        $index = array_search($row, $unique);

        foreach($exj AS $merge)
        {
            $list[$index][$merge] += $arr[$di][$merge];
        }
    }

    foreach($list AS $index => $row)
    {
        foreach($exj AS $merge)
        {
            $list[$index][$merge] = implode(',', $list[$index][$merge]);
        }
    }

    return array_values($list);
}

Usage

  • Param 1 (String) Key name of value to make unique.
  • Param 2 (Array) To Process.
  • Param 3 (Array) List of keys in processed array to make (String|Array) values a unique comma separated String.

    font_merge_unique('family', $array, array('weight','charset'))

Result

Array
(
    [0] => Array
        (
            [family] => Abril Fatface
            [weight] => regular
            [charset] => latin,latin-ext
        )

    [1] => Array
        (
            [family] => Akronim
            [weight] => regular,bold
            [charset] => latin,latin-ext
        )

    [2] => Array
        (
            [family] => Acme
            [weight] => regular
            [charset] => latin
        )

)
Saturday, May 29, 2021
 
Evernoob
answered 5 Months ago
13

There's a lot been written about PHPExcel and memory use, and I'm not going to repeat it all here.

Try reading some of the threads on the PHPExcel discussion board discussing the issue, such as this one; or previous answers here on SO such as this one or this one

Tuesday, July 6, 2021
 
Slinky
answered 4 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 :