00001 <?php 00002 /* vim: set expandtab tabstop=4 shiftwidth=4: */ 00003 // 00004 // Copyright (c) 2003 Laurent Bedubourg 00005 // 00006 // This library is free software; you can redistribute it and/or 00007 // modify it under the terms of the GNU Lesser General Public 00008 // License as published by the Free Software Foundation; either 00009 // version 2.1 of the License, or (at your option) any later version. 00010 // 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 // Lesser General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU Lesser General Public 00017 // License along with this library; if not, write to the Free Software 00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 // 00020 // Authors: Laurent Bedubourg <laurent.bedubourg@free.fr> 00021 // 00022 // $id$ 00023 00024 define('ALGO_MAP_VERSION', '0.1.1'); 00025 00145 function Algo_map() 00146 { 00147 // we retrieve the list of map arguments 00148 $argv = func_get_args(); 00149 // the first argument is the function name 00150 $func = array_shift($argv); 00151 00152 if (is_object($func)) { 00153 // if first argument is an object, this map 00154 // has a callback to some method 00155 $obj = $func; 00156 $func = array_shift($argv); 00157 } else if (!function_exists($func)) { 00158 // if the function isn't defined, 00159 // we'll try to create the function 00160 // the first argument was the string 00161 // of function arguments(ie:'$a,$b,$c') 00162 00163 // The code is the second argument 00164 $code = array_shift($argv); 00165 00166 // We create the new function 00167 // it will be a lambda style function 00168 $func = create_function($func, $code); 00169 } 00170 00171 // prepare the result array 00172 $result = array(); 00173 00174 // we'll apply the new function to 00175 // each element of arrays passed as 00176 // parameters 00177 while (count($argv[0]) > 0) { 00178 // prepare the array of arguments 00179 $fargs = array(); 00180 00181 for($i = 0; $i < count($argv); $i++) { 00182 // for each array to map 00183 // we add the next element of the 00184 // array to the argument array 00185 $fargs[] = array_shift($argv[$i]); 00186 } 00187 00188 if (isset($obj)) { 00189 $result = array_merge($result, 00190 call_user_func_array(array($obj,$func), 00191 $fargs)); 00192 } else { 00193 // we merge the result of the function 00194 // with allready found results 00195 $result = array_merge($result, 00196 // call the function with the array 00197 // of arguments 00198 call_user_func_array($func, $fargs)); 00199 } 00200 } 00201 00202 // let's return the result array 00203 return $result; 00204 } 00205 00206 ?>