Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

just full arrays for array_intersect... how 1

Status
Not open for further replies.

bateman23

Programmer
Mar 2, 2001
145
DE
Hello everybody,

i need to create the intersection of multiple arrays.
I'm aware that this is done with the array_intersect-function.

My problem is, that - at runtime - i don't know which arrays are filled and which are empty. So now the array_intersect throws an error-message "argument x is not an array" - Sure it does, it has to ;-)
My problem is: How do i just put the full arrays into the argument-line of array_intersect and discard the empty ones...?

Any help is greatly appreciated.
Thanx in advance,

Daniel Purucker
 
The trick is to understand that array intersections are associative operations. You don't have to perform the intersection in a single call of array_intersect().

This code will determine the intersection of a group of variables, checking to make sure that the items to be intersected are non-empty arrays:

Code:
<?php

print '<html><body><pre>';

$a = array(
	array (1, 2, 3, 4, 5),
	array (),
	array (3, 4, 5, 6, 7),
	array (5, 6, 7, 8, 9),
	3
);

$found_array = FALSE;
foreach ($a as $b)
{
	if (is_array($b) && count($b) > 0)
	{
		if ($found_array == FALSE)
		{
			$accumulator = $b;
			$found_array = TRUE;
		}
		else
		{
			$accumulator = array_intersect ($accumulator, $b);
		}
	}
}

print_r ($accumulator);

print '</pre></body></html>';
?>

Perhaps some variation on this theme could be useful to you.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
No problem.

The documentation doesn't say that you can use array_interset() this way. The only reason I figured it out was from knowing that the intersection of sets is associative.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top