Re: array_intersect question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I know there must be a more elegant way with array_reduce or something, but
I would simply write a function called

function array_intersect_m($m_array) {
  $intersection = $m_array[0];
  for ($i=1; $i < count($m_array); $i++) {
    $intersection = array_intersect($m_array[$i], $intersection);
  }
  return $intersection;
}

and put that into my library. O and while i'm at it, the array_reduce way
would prob be:
$m_array =
array(array("green","red","blue"),array("green","yellow","red"),array("green","red","purple"),array("green","red","yellow"));
array_reduce($m_array, 'array_intersect');

but this could be wrong, havent done much with these 'meta' functions.

Regards,
Tim


On Tue, Dec 2, 2008 at 10:24 PM, Andrej Kastrin <andrej.kastrin@xxxxxxxxx>wrote:

> Dear all,
>
> I have to perform an intersection on array of arrays. The fact is that php
> does not support intersection on multidimensional arrays.
>
> So, the first simple example using only one dimensional arrays works well:
>
> $array1 = array("green", "red", "blue");
> $array2 = array("green", "yellow", "red");
> $array3 = array("green", "red", "purple");
> $array4 = array("green","red","yellow");
> $result = array_intersect($array1,$array2,$array3,$array4);
> print_r($result);
>
> And the result is: Array ( [0] => green [1] => red )
>
> The question is how to perform intersection on the following structure:
>
> $products =
> array(array("green","red","blue"),array("green","yellow","red"),array("green","red","purple"),array("green","red","yellow"));
>
> Thanks in advance for any suggestions.
>
> Best, Andrej
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
Tim-Hinnerk Heuer

http://www.ihostnz.com -- Web Design, Hosting and free Linux Support

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux