Date: 07/12/06 (PHP Community) Keywords: php, database PHP - function joinData($array1, $array2) { $len1 = count($array1); //length of array 1 - speeds up loops $len2 = count($array2); //length of array 2 $keys1 = array_keys($array1); //keys of array1 $vals1 = array_values($array1); //values of array 1 $keys2 = array_keys($array2); //keys of array2 $vals2 = array_values($array2); //values of array 2 $output = array(); //what we send out for($i=0;$i<$len1;$i++) { //foreach element in array 1 for($j=0;$j<$len2;$j++) { //search each element in array 2 if( ($keys1[$i] == ereg_replace("frm_","",$keys2[$j])) ) { //until we find a key that matches after stripping frm_ $output[$keys1[$i]] = $vals2[$j]; // overwrite the value in array1; array2 takes precidence break; // go to next element in array 1 - speeds up process (function is O(n log n), instead of O(n^2)) } } } return $output; //send back the new values }
|