note 13927 deleted from function.array-unique by cmb
| From: | cmb@php.net | Date: | Sun, 27 Oct 2019 08:45:48 +0000 |
| Subject: | note 13927 deleted from function.array-unique by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212807@lists.php.net to get a copy of this message | ||
Note Submitter: az at top-webdesign dot de
----
Attention!
If you use array_unique be aware of data-types! (I spent hours of debugging because of that ...).
For example, if you've got an array containing a '3' as number and another
'3' as string it won't be eliminated by array_unique.
An Example where this can happen, without really thinking about it:
I've got an article-list with product-numbers where the third and fourth digit is the code for
the producer. So I read in the file an process it line by line and put each producer-code into an
array:
------------------------------
<?php
$i=0;
while($line = fgets($csv, 10000) {
// splitting the line, product_no is the first part:
$data = explode(";", $line);
// putting the producer_code into an array:
$producer_id[$i] = trim(substr($data[0], 2, 2));
// make a special exception:
if(trim(substr($data[0], 2, 2)) == 40) {
$producer_id[$j] = '30';
}
// in the above line if you leave the 30 without the ''
// array_unique won't work!
$i++;
}
$producer_ids = array_values(array_unique($producer_id));
?>
-------------------------------
Result is to have all producer-ID's in an array without dupes.