Re: [PHP] SELECT into array of arrays

2008-12-03 Thread Andrej Kastrin

Thanks Yeti, it works.

Best, Andrej

Yeti wrote:

Correcting myself now ..

$myArray = array('b2005', 'b2008');
$sql = SELECT study,symbol FROM test WHERE study IN ('$myArray[$i]');
$result = mysql_query($sql);

if(mysql_num_rows($result)  0) {
  while ($myrow = mysql_fetch_array($result)) {
  if (in_array($myrow['study'], $myArray))
$combinedArray[$myrow['study']][] = $myrow['symbol'];
  }
}

Forgot to get rid of the first for loop


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



[PHP] array_intersect question

2008-12-02 Thread Andrej Kastrin

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



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] mailto:[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



[PHP] SELECT into array of arrays

2008-12-02 Thread Andrej Kastrin

Dear all,

I have a MySQL table 'test' which includes two columns: 'study' and 
'symbol':


study symbol
a2008 A
a2008 B
a2008 C
a2008 D
b2005 A
b2005 B
b2005 E


Using POST variable I passed 'study' values into $myArray:

// $myArray is variable length; used only two values in example
$myArray = array(a2008,b2005);


Then I try to produce the following structure (array of arrays):

$combinedArray = array(array('A','B','C','D'),array('A','B','E'));


Below is my php script. In the present solution the $combinedArray 
includes only 'symbol' values for last iteration (where $myArray = b2005).


How should I proceed? Thanks in advance for any suggestions.

Best, Andrej



$combinedArray = array();
for ($i=0;$icount($myArray);$i++) {

$sql = SELECT study,symbol FROM test WHERE study IN ('$myArray[$i]');
$result = mysql_query($sql);

if(mysql_num_rows($result)  0) {
while ($myrow = mysql_fetch_array($result)) {
$combinedArray[] = $myrow['symbol'];
}
}
}

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