benjaminbeazy Posted March 5, 2007 Share Posted March 5, 2007 i am still newb at functions and classes im trying to pass a multilevel array to a function of a class, such as this.. $array1 = array("w" => "value", "x" => "value2"); $array2 = array("y" => "value3", "z" => "value4"); $search = new Search(); $search->query($array1, $array2); is that the correct way to pass an array, and how do i access the arrays, contents, and original key names (w,x,y,z) within the function currently i have something like this for my class/function class Search{ function query(){ $args = func_get_args(); foreach($args as $var => $val){ echo "$var = $val"; } } } and that gives me: 0 = Array 1 = Array how do i get it to output: array 1 w = value1 x = value2 array 2 y = value3 z = value4 ??? all help is very much appreciated, p.s. i have access to both php 4 & 5 and have no preference for this Link to comment https://forums.phpfreaks.com/topic/41248-solved-passing-multilevel-array-to-function-output/ Share on other sites More sharing options...
benjaminbeazy Posted March 5, 2007 Author Share Posted March 5, 2007 *bump* Link to comment https://forums.phpfreaks.com/topic/41248-solved-passing-multilevel-array-to-function-output/#findComment-199880 Share on other sites More sharing options...
kenrbnsn Posted March 5, 2007 Share Posted March 5, 2007 First, those are not multi-dimensioned arrays, each of those are single dimensioned arrays. Second, when you define a function, you should tell PHP how many parameters to expect, then you can used them as normal variables: <?php class Search{ function query($ary1,$ary2){ echo '$ary1:<br>'; foreach ($ary1 as $k => $v) echo $k . ' = ' . $v . '<br>'; echo '$ary2:<br>'; foreach ($ary2 as $k => $v) echo $k . ' = ' . $v . '<br>'; } } ?> Ken Link to comment https://forums.phpfreaks.com/topic/41248-solved-passing-multilevel-array-to-function-output/#findComment-199918 Share on other sites More sharing options...
benjaminbeazy Posted March 5, 2007 Author Share Posted March 5, 2007 i used func_get_args as the amount of arguments may change i guess i was simplifying it a little, the array would actually be $array = array(array(x => 1, y=> 2), array(z => 3, w => 4)); Link to comment https://forums.phpfreaks.com/topic/41248-solved-passing-multilevel-array-to-function-output/#findComment-199920 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.