macross Posted June 18, 2012 Share Posted June 18, 2012 So this is an example how i would like to get values from array inside a function. ( Note that array is an unknown size.. It might be empty or it might contain 3 keys) $data = array(1=>"hello", 2=>"welcome"); function doSomething($value1, $value2){ //Do something } doSomething($data) How can i achieve such thing? Quote Link to comment https://forums.phpfreaks.com/topic/264367-passing-array-to-a-function/ Share on other sites More sharing options...
Kieran Menor Posted June 18, 2012 Share Posted June 18, 2012 And this won't suffice? $data = array(1=>"hello", 2=>"welcome"); function doSomething($array) { // $array[1] = hello , $array[2] = welcome ... etc. } doSomething($data) Quote Link to comment https://forums.phpfreaks.com/topic/264367-passing-array-to-a-function/#findComment-1354788 Share on other sites More sharing options...
macross Posted June 18, 2012 Author Share Posted June 18, 2012 I would like it to function like in codeigniter when passing Ids to methods you can do this: class Home extends Controller{ public function __construct(){ } public function index($id1, $id2){ echo $id1; echo $id2; } } Quote Link to comment https://forums.phpfreaks.com/topic/264367-passing-array-to-a-function/#findComment-1354798 Share on other sites More sharing options...
MaaSTaaR Posted June 18, 2012 Share Posted June 18, 2012 I'm not sure how CodeIgniter works but I think you need func_get_args : http://php.net/manual/en/function.func-get-args.php Quote Link to comment https://forums.phpfreaks.com/topic/264367-passing-array-to-a-function/#findComment-1354799 Share on other sites More sharing options...
Kieran Menor Posted June 18, 2012 Share Posted June 18, 2012 Or depending on what exactly you mean, call_user_func_array might be what you are looking for. Quote Link to comment https://forums.phpfreaks.com/topic/264367-passing-array-to-a-function/#findComment-1354813 Share on other sites More sharing options...
macross Posted June 18, 2012 Author Share Posted June 18, 2012 Or depending on what exactly you mean, call_user_func_array might be what you are looking for. Thanks call_user_func_array() works perfectly! Quote Link to comment https://forums.phpfreaks.com/topic/264367-passing-array-to-a-function/#findComment-1354831 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.