note 130221 added to function.array-intersect

From: Date: Thu, 03 Apr 2025 15:11:15 +0000
Subject: note 130221 added to function.array-intersect
Groups: php.notes 
Request: Send a blank email to php-notes+get-222057@lists.php.net to get a copy of this message
It preserves from the first array: - keys - order of elements - duplicates Duplicates: note that if the first array has just one value, and the other array has two of those values, only one will be returned, but in the opposite case, two will be returned. Order of elements: it's preserved from the first array even if integer keys are out of order. Example: ________________________________ <?php $array1 = ['a' => 'cherry', 'b' => 'banana', 'c' => 'cherry']; $array2 = [1 => 'banana', 0 => 'cherry', 2 => 'date']; $result1 = array_intersect($array1, $array2); $result2 = array_intersect($array2, $array1); echo "Result from array_intersect(\$array1, \$array2):\n"; print_r($result1); echo "\nResult from array_intersect(\$array2, \$array1):\n"; print_r($result2); ?> ___________________________________________ Outputs: Result from array_intersect($array1, $array2): Array ( [a] => cherry [b] => banana [c] => cherry ) Result from array_intersect($array2, $array1): Array ( [1] => banana [0] => cherry ) ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.137 (proxied: 161.53.254.120) ---- Manual Page -- https://php.net/manual/en/function.array-intersect.php Edit -- https://main.php.net/note/edit/130221 Del: integrated -- https://main.php.net/note/delete/130221/integrated Del: useless -- https://main.php.net/note/delete/130221/useless Del: bad code -- https://main.php.net/note/delete/130221/bad+code Del: spam -- https://main.php.net/note/delete/130221/spam Del: non-english -- https://main.php.net/note/delete/130221/non-english Del: in docs -- https://main.php.net/note/delete/130221/in+docs Del: other reasons-- https://main.php.net/note/delete/130221 Reject -- https://main.php.net/note/reject/130221 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#222057) next »