Asked  9 Months ago    Answers:  5   Viewed   50 times

I have two arrays. They seem to contain at least one identical set of values, but performing array_diff() does not return anything even though I think it should! This should have been just routine code but for some reason it's not liking what I've done.

The weird thing is that var_dump($queue[0]); returns String(167); and var_dump($videos[0]) returns String(168).

So clearly, they must be different right?

echo similar_text($queue[0]), $videos[0]); returns 167. What!?

Note: These are just file names and do not represent the contents of the file.

Videos Array

Array ( [0] => /var/www/downloads/j2/Dexter Season 1, 2, 3, 4, 5 & 6 + Extras (Early Cuts, Audiobooks etc) DVDRip HDTV TSV/Season 3/Dexter Season 3 Episode 04 - All in the Family.avi )

Queue Array

Array ( [0] => /var/www/downloads/j2/Dexter Season 1, 2, 3, 4, 5 & 6 + Extras (Early Cuts, Audiobooks etc) DVDRip HDTV TSV/Season 3/Dexter Season 3 Episode 04 - All in the Family.avi [1] => j2 )

Outputs

$diff = array_intersect($queue,$videos); print_r($diff); returns Array ( )

var_dump($queue[0]); returns string(167) "/var/www/downloads/j2/Dexter Season 1, 2, 3, 4, 5 & 6 + Extras (Early Cuts, Audiobooks etc) DVDRip HDTV TSV/Season 3/Dexter Season 3 Episode 04 - All in the Family.avi"

var_dump($videos[0]); returns string(168) "/var/www/downloads/j2/Dexter Season 1, 2, 3, 4, 5 & 6 + Extras (Early Cuts, Audiobooks etc) DVDRip HDTV TSV/Season 3/Dexter Season 3 Episode 04 - All in the Family.avi"

echo similar_text($queue[0], $videos[0]); returns 167.

I've put the strings into JavaScript character counts, I've used strlen(), trim() to trim whitespace, I've even manually counted each character individually. What's going on?

 Answers

85

After converting both strings to hex-escaped form using

var_dump(preg_replace_callback('#.#', function($m) {
  return '\x' . dechex(ord($m[0]));
}, $input))

, the result strings appear like this: http://jsfiddle.net/mgaWn/

Looking at them in that form shows that the first string contains 5,·6·+·Extras, the second one contains 5,·6··+·Extras - there's a double space before the + sign.

HTML collapses whitespace and this difference becomes completely invisible. It is generally a good idea to compare the data as close to its original format as possible, before any output format specifics (such as character encodings or this HTML whitespace minimization) get in your way.

Wednesday, March 31, 2021
 
Troncoso
answered 9 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 7 Months ago
55

Arrays in Java store one of two things: either primitive values (int, char, ...) or references (a.k.a pointers).

So, new Integer[10] creates space for 10 Integer references only. It does not create 10 Integer objects (or even free space for 10 Integer objects).

Incidentally that's exactly the same way that fields, variables and method/constructor parameters work: they too only store primitive values or references.

Monday, June 14, 2021
 
fret
answered 6 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 5 Months ago
58

The reason is that statistic about message sended are updated in real-time, instead received, impression and message opened do not. I get these statistic the next day.

Please note that for "Data Message" you have to set in the message the field "analytics_label" in order to see statistics. For "Notification message" this should not be mandatory to see statistics.

Friday, November 5, 2021
 
Arthur Ulfeldt
answered 1 Month 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 :
 
Share