Forum Moderators: coopster

Message Too Old, No Replies

Arrays and php to compare records

         

varunkrish

11:58 am on Jun 8, 2005 (gmt 0)

10+ Year Member



I want to build a comparison script which can compare upto 5 rows at a time.
----------------------------
¦ 1 ¦ 2 ¦ 3 ¦ 4 ¦ 5 ¦
-----------------------
¦ ¦ ¦ ¦ ¦ ¦
Currently i am using a simple code to compare just two rows..no arrays nothing.. i pass just two variables thats it..and fetch the records and a print output :)

I found out that using arrays u can compare records in php..and that the no of records can be checked using the
"foreach" function

How do i start...?

I am clueless

mcibor

1:41 pm on Jun 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



function compare($array1, $comparison, $array2){
if(count($array1)!= count($array2)) return false;
foreach($array1 as $name => $value){
if(eval($array1[$name]." ".$comparison." ".$array2[$name])) $output[$name] = 1; else $output[$name] = 0;
}
return $output;
}

$a = array("1", "2", "1", "2");
$b = array("0", "1", "2", "3");
$greater = compare($a, ">", $b);//returns an array 0, 0, 1, 1

Hope this helps;
But remember this works on association, not on order. And both arrays need to be the same length. Also you should check if $comparison is not sth malicious
Best regards
Michal Cibor

varunkrish

3:06 pm on Jun 8, 2005 (gmt 0)

10+ Year Member



thanks..got an idea now..