Re: [PHP] array_intersect question

2008-12-10 Thread German Geek
On Tue, Dec 2, 2008 at 11:06 PM, Andrej Kastrin <[EMAIL PROTECTED]>wrote:

> It works like a charm.
>
> Thanks, Andrej
>
> Tim | iHostNZ wrote:
>
>> 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');
>>
>
I tried this now with array_reduce and it didn't work as i expected. Can
anyone tell me why?
It says argument #1 to array_intersect is not an array, although i have an
array of arrays. Also tried providing $arrayOfArrays[0] as the third
parameter to array_reduce which had the same error.

Thanks,
Tim


Re: [PHP] array_intersect question

2008-12-02 Thread Andrej Kastrin

It works like a charm.

Thanks, Andrej

Tim | iHostNZ wrote:

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 
<[EMAIL PROTECTED] > 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


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] array_intersect question

2008-12-02 Thread Tim | iHostNZ
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 <[EMAIL PROTECTED]>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


Re: [PHP] array_intersect question

2008-12-02 Thread Yeti
> 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"));

If I understood you correctly ..



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] array_intersect problem

2006-12-25 Thread Martin Alterisio

2006/12/25, Leo Liu <[EMAIL PROTECTED]>:


Hi,

  I try to intersect associative array and it seems to fail to do so. Can
anyone show me a walk around?

  For example I have

  array1

  Array
(
[0] => Array
(
[imageID] => 1
)

[1] => Array
(
[imageID] => 2
)

[2] => Array
(
[imageID] => 3
)

[3] => Array
(
[imageID] => 4
)
)

  And array 2

  Array
(
[0] => Array
(
[imageID] => 6
)

[1] => Array
(
[imageID] => 3
)
)

  After intersection instead of me getting 3 as a result, I got the array
1 unchanged. Seems like intersection doesn't take place at all. Anyway to
solve this problem?

  Regards,

  Leo




Reality starts with Dream



Quote from php manual, in the reference for the array_intersect function:

*Note: * Two elements are considered equal if and only if (string) $elem1
=== (string) $elem2. In words: when the string representation is the same.

That's why array_intersect isn't working with your arrays.

If you're using PHP5 you could use array_uintersect, and compare the items
by the imageID:

   function compareByImageID($elem1, $elem2) {
   return $elem1['imageID'] - $elem2['imageID'];
   }

   $intersection = array_uinsersect($array1, $array2, 'compareByImageID');

Or you can do the following, which works in PHP4, but is not as versatile as
array_uintersect:

   class InArrayFilter {
   var $arr;

   function InArrayFilter($arr) {
   $this->arr = $arr;
   }

   function filterFunction($elem) {
   return in_array($elem, $this->arr);
   }
   }

   $filter = new InArrayFilter($array2);
   $intersection = array_filter($array1, array($filter, 'filterFunction'));


Re: [PHP] array_intersect()

2001-01-19 Thread Chris Lee

ok, Ive got my code to work, but its a big jerry-rig haha. because I am
using mtime() (the last two chars are allways 00) I substr the last two
chars off, array_insersect() them, then add 00 the the end of all the values
in the result array. dumb eh.

Chris Lee
Mediawaveonline.com



"Chris Lee" <[EMAIL PROTECTED]> wrote in message
94abie$j8q$[EMAIL PROTECTED]">news:94abie$j8q$[EMAIL PROTECTED]...
>
> Ok I thought array_intersect() was the function I was looking for, I might
> have to write my own.
>
> I want an array with ONLY the keys/values that are present in all the
> variables; ie.
>
>  $test_a[] = '974806802083190501';
> $test_a[] = '974806802083190503';
> $test_a[] = '974806802083190504';
>
> $test_b[] = '974806802083190501';
>
> $new_array = array_intersect($test_a, $test_b);
>
> print_r($new_array);
> ?>
>
> Array
> (
> [0] => 974806802083190501
> [1] => 974806802083190503
> [2] => 974806802083190504
> )
>
>  $test_a[] = '501';
> $test_a[] = '503';
> $test_a[] = '504';
>
> $test_b[] = '501';
>
> $new_array = array_intersect($test_a, $test_b);
>
> print_r($new_array);
> ?>
>
> Array
> (
> [0] => 501
> )
>
> array_insersect() only seems to work with small numbers... or small text.
>
> Chris Lee
> Mediawaveonline.com
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> To contact the list administrators, e-mail: [EMAIL PROTECTED]
>



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]