Primal Posted September 16, 2007 Share Posted September 16, 2007 How do I pass a part of a multidimensional array to a function. I have the following (in pseudo): $arrays[m][n]; // filled with values $chosen = 3; function doSomething($array) { // do something with 1D array } This fails: doSomething($arrays[$chosen]); Can someone explain to me why? And how to do this right? Quote Link to comment https://forums.phpfreaks.com/topic/69579-solved-passing-array-of-multidimensional-array-to-function/ Share on other sites More sharing options...
Jessica Posted September 16, 2007 Share Posted September 16, 2007 What do you mean "fails" Quote Link to comment https://forums.phpfreaks.com/topic/69579-solved-passing-array-of-multidimensional-array-to-function/#findComment-349657 Share on other sites More sharing options...
rarebit Posted September 16, 2007 Share Posted September 16, 2007 This passes a single array: doSomething($arrays[$chosen]); whereas this would pass the full multidimensional array: doSomething($arrays); Quote Link to comment https://forums.phpfreaks.com/topic/69579-solved-passing-array-of-multidimensional-array-to-function/#findComment-349666 Share on other sites More sharing options...
cooldude832 Posted September 16, 2007 Share Posted September 16, 2007 You can pass any data type into a function (String, Integer, Array) as long as its functionality in the function reflects its type. example if I pass the string of Yes I can to a function and then try and multiple it by some value it will value. The same holds true if you don't handle an array in the function properly Quote Link to comment https://forums.phpfreaks.com/topic/69579-solved-passing-array-of-multidimensional-array-to-function/#findComment-349670 Share on other sites More sharing options...
Primal Posted September 16, 2007 Author Share Posted September 16, 2007 I want to pass a single array, but somehow it doesnt do what I want with $arrays[$chosen]. The function does function with a normal 1D array. So, you all are saying that $arrays[$chosen] should pass a single array? Quote Link to comment https://forums.phpfreaks.com/topic/69579-solved-passing-array-of-multidimensional-array-to-function/#findComment-349709 Share on other sites More sharing options...
Dragen Posted September 16, 2007 Share Posted September 16, 2007 yes, or perhaps you even need to use: $array[$chosen][$chosen] depending on what you're wanting to do. Quote Link to comment https://forums.phpfreaks.com/topic/69579-solved-passing-array-of-multidimensional-array-to-function/#findComment-349711 Share on other sites More sharing options...
rarebit Posted September 16, 2007 Share Posted September 16, 2007 Show your code from within the function... Quote Link to comment https://forums.phpfreaks.com/topic/69579-solved-passing-array-of-multidimensional-array-to-function/#findComment-349713 Share on other sites More sharing options...
Primal Posted September 16, 2007 Author Share Posted September 16, 2007 I rechecked other areas for potential errors and found one - my code functions as it should now. Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/69579-solved-passing-array-of-multidimensional-array-to-function/#findComment-349714 Share on other sites More sharing options...
rarebit Posted September 16, 2007 Share Posted September 16, 2007 http://www.sitepoint.com/article/advanced-email-php http://www.sitepoint.com/article/code-html-email-newsletters Quote Link to comment https://forums.phpfreaks.com/topic/69579-solved-passing-array-of-multidimensional-array-to-function/#findComment-349715 Share on other sites More sharing options...
rarebit Posted September 16, 2007 Share Posted September 16, 2007 ops wrong thread! Quote Link to comment https://forums.phpfreaks.com/topic/69579-solved-passing-array-of-multidimensional-array-to-function/#findComment-349716 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.