note 16213 added to function.array-intersect
| From: | lectrix at gmx dot net | Date: | Sat, 20 Oct 2001 16:33:47 +0000 |
| Subject: | note 16213 added to function.array-intersect | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-19203@lists.php.net to get a copy of this message | ||
someone might need to generate the parameter head of array_intersect in runtime (eg: not always the
same number of arrays, or the same arrays...). i was looking and asking for a solution to this
problem but noone could really give me anything more efficent (and shorter) than this one:
//these are your arrays you want to intersect:
$arr1=array("A","B","C","D","E");
$arr2=array("D","E","C","G","H","I","J");
$arr3=array("A","D","E","G","H","K");
$arr4=array("E","B","H","D","K","L");
//solution: D E
//this is your function parameter-head, you can generate this however you want:
$params="arr1,arr2,arr3,arr4";
$arr_arr=explode(",",$params);
$anz=sizeof($arr_arr);
for ($i=0;$i<$anz-1;$i++ )
{
${$arr_arr[$i+1]}=array_intersect(${$arr_arr[$i]},${$arr_arr[$i+1]});
}//for
$intersect=${$arr_arr[$anz-1]};
foreach ($intersect as $elem )
{
echo $elem." ";
}//foreach
maybe someone needs this, ... maybe not, maybe even you know a better and shorter way (let me
know!!).
cya, folks!
lectrix.
--
http://www.php.net/manual/en/function.array-intersect.php
http://master.php.net/manage/user-notes.php?action=edit+16213
http://master.php.net/manage/user-notes.php?action=delete+16213
http://master.php.net/manage/user-notes.php?action=reject+16213