Using array_intersect with an unknown number of arrays
| From: | robert mena | Date: | Thu, 06 May 2010 19:27:44 +0000 |
| Subject: | Using array_intersect with an unknown number of arrays | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-304966@lists.php.net to get a copy of this message | ||
Hi,
I have a result set coming from a database that can be of the form below
$lines = array(0 => array('idA' => 1, 'idU' => 1),
1 => array('idA' => 1, 'idU' => 2),
2 => array('idA' => 2, 'idU' => 1),
3 => array('idA' => 2, 'idU' => 2),
4 => array('idA' => 3, 'idU' => 1),
5 => array('idA' => 3, 'idU' => 2));
I used the code to generate arrays based on the idA, i.e, one array
containing all the idU from each idA
foreach ($lines as $r)
{
if(!isset($list[$r['idA']]))
{
$list[$r['idA']] = array();
}
array_push($list[$r['idA']], $r['idU']);
}
Now need just the idU that occurs in all.
In my example I could simply
$x = array_intersect($list[1], $list[2], $list[3]);
This works but the problem is that I do not know in advance how many (and
which) indexes I will get from that foreach since the results will differ
from the database.
Is there a way around this ?