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? 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) 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; } } 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 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. 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! 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
Archived
This topic is now archived and is closed to further replies.