1

I've got an array like this

array('item1', 'item2', 'item3');

Does anybody know how I would pass each item into a function seperately?

EG:

hello_world('item1', 'item2', 'item3');
3

3 Answers 3

6
hello_world($array[0], $array[1], $array[2])

or

call_user_func_array('hello_world', $array)

http://php.net/call_user_func_array

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

1 Comment

Thank you! $array[0] ... etc wouldn't help here as it needs to be dynamic. I'm passing items from the URL into the function so with /MyAction/hello/world and MyAction($item1, $item2): $item1 would be hello and $item2 would be world
4

http://www.php.net/manual/en/function.call-user-func-array.php

call_user_func_array('func',$myArgs);

Comments

0

The array_map function can help you http://www.php.net/manual/es/function.array-map.php

5 Comments

How can array_map help here?
Using array_map, it tells the function you want to apply to each item in the array. It's what I've understood that Andy needs.
Perhaps I have misunderstood the question. Whats the difference with call_user_func_array function?
I do not agree with the negative that has been given to my answer. Is a valid response based on the vision interprets the question.
I wanted to pass the array into the function. Not map each of the arrays values to the output of the function.

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.