Forum Moderators: coopster
$array($val1, $val2, $val3, $val4, ...);
into this:
my_function($val1, $val2, $val3, $val4, ... );
There's all the func_num_args(), etc. but I'm not sure how to "explode" the array into the function list.
Has anyone had experience with something like this before?
Thanks!
-sned
<?php
function myfunction($arr){
if(is_array($arr)){
print_r( $arr );
} else {
echo 'Not an array!';
}
}
$array = array('red','blue','green');
myfunction($array);
I'm trying to abstract the mysqli_stmt_bind_param [us.php.net] function to something like this:
function db_bind_param($stmt, $array){
$types = '';
foreach($array as $value=>$type){
$types .= $type;
// do something with the value here to get $val1, $val2, $val3, etc.
}
return mysqli_stmt_bind_param($stmt, $types, $val1, $val2, $val3, ....);
}
Where $array is something like:
$array = array(
'this is my string'=>'s',
'12'=>'i'
);
At least it was a good learning experience with eval (and those variable variables always twist my mind around).
// bind parameters to a statement: params are sent as array($val=>'i', $val=>'d', etc...)
function db_stmt_bind_param($stmt, $params){
$types = '';
$values = array();
$x = 0;
foreach($params as $val=>$type){
$types .= $type;
$varname = 'var' . $x;
$$varname = $val;
$values[] = '$' . $varname;
$x++;
}
$temp = implode(',', $values);$stmtvar = 'mystmt';
$$stmtvar = $stmt;return eval("return mysqli_stmt_bind_param($$stmtvar, '$types', $temp);");
}
Thanks again for your help.
-sned
Much cleaner:
function db_stmt_bind_param($stmt, $params){
$opts[] = $stmt;
foreach($params as $val=>$type){
$opts['type'] .= $type;
$opts[] = $val;
}
return call_user_func_array('mysqli_stmt_bind_param', $opts);
}