0

Let's say I have an array like this

$update = array(
  'field1' => 'one',
  'field2' => 'two',
  'field3' => 'three'
);

Then I have a function, let's say write() which accepts unlimited number of arguments like this write($thing_one,$thing_two,$thing_three,$thing_four)

So I know I can pass arguments like write($update[0],$update[1],$update[2]) but in real example I have a lot of keys and values in $update and what I want to do is find a way to pass arguments from this array to a function, so that it would be same as manually typing update($update[0],$update[1],$update[2])

I hope somebody will help me out. Thank you.

7
  • 2
    Why not pass the whole array? Commented Nov 18, 2012 at 14:35
  • It sounds to me like you are getting confused partly because of the way you set up your applications logic. This really shouldn't be an issue as people would usually just assemble an array of parameters to pass as a whole. I suggest you reconsider your logic here (or alternatively explain it abit more so that we can give suggestions) Commented Nov 18, 2012 at 14:36
  • just set your write() function to receive one variable and then retrieve individually inside the function by means of an index Commented Nov 18, 2012 at 14:41
  • write does not accept an array. It accepts many arguments. To better understand this it is coming from mysqli class. example: $dbi->prepare("INSERT INTO table (name,surname) VALUES (?,?);")->execute('john','davids'); So I can create (name,surname) from array, and (?,?) as well, but cannot create 'john','davids' as arguments for execute function. thanks Commented Nov 18, 2012 at 14:42
  • pass whole array values by using foreach.... Commented Nov 18, 2012 at 14:44

2 Answers 2

1

call_user_func_array() should do the trick nicely.

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

1 Comment

This does job. Thanks. $stmt = $this->dbi->prepare($sql); $stmt = call_user_func_array(array($stmt, "execute"), $args);
0

Why not just pass the array itself to the write function:

$update = array(
  'field1' => 'one',
  'field2' => 'two',
  'field3' => 'three'
);

write($update);

5 Comments

since when php is type safe?
yes write does not accept an array. It accepts many arguments. To better understand this it is coming from mysqli class. example: $dbi->prepare("INSERT INTO table (name,surname) VALUES (?,?);")->execute('john','davids'); So I can create (name,surname) from array, and (?,?) as well, but cannot create 'john','davids' as arguments for execute function. thanks
@ftom2 Try passing an array to echo. By your logic it should be the same as passing the individual values.
@Kolink - I am assuming write() is a function he wrote, not a built in function, and as he mentioned it accepts many arguments, so it seems better to just send an array and if needed change the functionality a bit to accommodate to it.
That's how I managed to do this: $stmt = $this->dbi->prepare($sql); $stmt = call_user_func_array(array($stmt, "execute"), $args);

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.