note 128307 added to function.array-diff
| From: | Sbastien at main dot php dot net | Date: | Mon, 13 Mar 2023 21:23:11 +0000 |
| Subject: | note 128307 added to function.array-diff | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220264@lists.php.net to get a copy of this message | ||
I use array_diff() and array_intersect() to check the completness of a CSV file before processing :
<?php
$header = fgetcsv($stream, ...$csv_options); // ['id', 'name', 'sex']
$awaited = ['id', 'name', 'email']; // Header cols I need in the CSV
$present = array_intersect($header, $awaited); // ['id', 'name']
if (count($present) < count($awaited)) { // 2 < 3 => true, problem!
$missing = array_diff($present, $awaited); // ['email']
$missing = join(', ', $missing);
echo "Missing cols: {$missing}\r\n";
}
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.13.130 (proxied: 2a01:e0a:968:1840:2896:1dd5:3401:8bdb)
----
Manual Page -- https://php.net/manual/en/function.array-diff.php
Edit -- https://main.php.net/note/edit/128307
Del: integrated -- https://main.php.net/note/delete/128307/integrated
Del: useless -- https://main.php.net/note/delete/128307/useless
Del: bad code -- https://main.php.net/note/delete/128307/bad+code
Del: spam -- https://main.php.net/note/delete/128307/spam
Del: non-english -- https://main.php.net/note/delete/128307/non-english
Del: in docs -- https://main.php.net/note/delete/128307/in+docs
Del: other reasons-- https://main.php.net/note/delete/128307
Reject -- https://main.php.net/note/reject/128307
Search -- https://main.php.net/manage/user-notes.php