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');
andy
  • 2,369
  • 2
  • 31
  • 50
  • 2
    http://stackoverflow.com/questions/1467350/turn-array-into-independent-function-arguments-howto – Jose Vega Sep 05 '12 at 16:08
  • That didn't appear in any search results I received. – andy Sep 05 '12 at 16:21
  • 1
    possible duplicate of [Passing an Array as Arguments, not an Array, in PHP](http://stackoverflow.com/questions/744145/passing-an-array-as-arguments-not-an-array-in-php) – Fury Aug 13 '15 at 14:04

3 Answers3

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

or

call_user_func_array('hello_world', $array)

http://php.net/call_user_func_array

deceze
  • 510,633
  • 85
  • 743
  • 889
  • 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 – andy Sep 05 '12 at 16:20
4

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

call_user_func_array('func',$myArgs);
Abid Hussain
  • 7,724
  • 3
  • 35
  • 53
0

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

Lobo
  • 4,001
  • 8
  • 37
  • 67
  • How can `array_map` help here? – deceze Sep 05 '12 at 16:06
  • 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. – Lobo Sep 05 '12 at 16:07
  • Perhaps I have misunderstood the question. Whats the difference with call_user_func_array function? – Lobo Sep 05 '12 at 16:08
  • 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. – Lobo Sep 05 '12 at 16:10
  • I wanted to pass the array into the function. Not map each of the arrays values to the output of the function. – andy Sep 05 '12 at 16:18