1

so suppose I have a function:

function j($a, $b){
 return $a + $b;
}

and then I put the intended arguments of the function into a string:

$str = '1,3';

is there a way to make the function treat the single string argument as if it were the arguments that the programmer inserted into the function....so when you do

j($str),

instead of having the function treat the $str as a single string argument, it fetches the content of the string and treats it as if the programmer wrote j(1,3) instead of j($str)

also this is a rather simple example, but I'd like it to work for even more complicated argument strings involving arrays, multidimensional arrays, associative arrays, array within arrays, as well as string arguments that have commas in it (so just exploding by comma is not really feasible)

also I'd rather not use eval() in the solution

EDIT

Also I'm trying to get this to work on any function not just mine (and not just this specific function which is just a worthless example function) so preferably the operation is to be done outside of the function

6 Answers 6

1
call_user_func_array('j', explode(',', $str));

http://www.php.net/call_user_func_array

I have no idea how you want to make this work for "more complex argument strings including arrays of arrays", since it's hard to express those as strings. If you can format whatever string you have into an array though, for example using JSON strings and json_decode, call_user_func_array is your friend.

Sign up to request clarification or add additional context in comments.

2 Comments

eg the string 'array(array(1))' should be evaluated as one variable that is an array that contains another array that contains 1
To do that without eval you will need some custom string parsing function. You could easily go for json_decode('[[1]]', true) though. In general, literal PHP is not exactly the best choice for expressing complex data structures as strings. For that, use formats that were made for this like JSON or serialized PHP.
1

This should work for a single, comma-separated string:

function j($str){
 list($a, $b) = explode(',', $str);
 return $a + $b;
}

What do you want to sum in a multi-dimensional array? All of the values?

If yes: You only have to check the type of your argument (e.g. using is_array()) and then iterate through it. When it is multi-dimensional, call your function recursively.

2 Comments

see: also this is a rather simple example, but I'd like it to work for even more complicated argument strings involving arrays, multidimensional arrays, associative arrays, array within arrays, as well as string arguments that have commas in it (so just exploding by comma is not really feasible)
@kamikaze_pilot This needs to be done on a per-case basis, if you give us a real world example of what you are trying to achieve, we can help - but for any given situation the answer will probably be totally different to another...
1

make all parameter but except the first one optional and then use list() and explode() if the first parameter is a string (or contains ,):

function j($a, $b=null){
  if(strpos($a,',')!==false){
    list($a,$b) = explode(',',$a)
  }
  return $a + $b;
}

this is just a basic example, but the principle should be clear:

  • check if one of the arguments is composed of multiple parameters
  • if so, parse it on your own to get the single parameters

Comments

1
function j($a, $b = 0){ // make second arg optional
  if (! $b) { // is second arg specified and not zero
    if (strpos($a, ',') !== false){ // has provided first arg a comma
       list($first, $second) = explode(',' $a); // yes then get two values from it
       return $first + $second; // return the result
    }
  }
  else {
     return $a + $b; // two args were passed, return the result
  }

}

Now your function will support both formats, with one argument eg:

$str = '1,3'
j($str);

as well as two arguments:

j(5, 10);

Comments

0

This works :)

function f($str, $delimiter = ';')
{
    $strargs = explode($delimiter, $str);
    $args = array();
    foreach($strargs as $item)
        eval("\$args[] = " . $item. ";");
    // $args contains all arguments
    print_r($args);
}

Check it:

f("6;array(8,9);array(array(1,0,8), 5678)");

Comments

0

Most of the answers assume, that everything is nicely separated with coma ",", but it's not always the case. For example there could be different variable types, even strings with coma's.

$values = "123, 'here, is, a, problem, that, can\'t, be, solved, with, explode() function, mkay?'";

This can be handled with eval() and dot's "..." args retrieval in php 7.

function helper_string_args(...$args) {
    return $args;
}

$func = "\$values = \\helper_string_args($values);";
try {
    eval($func);
} catch (Exception $e) {
    var_dump($e);
    exit;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.