John_A Posted February 16, 2011 Share Posted February 16, 2011 I thought this would be fairly straightforward but for some reason isn't working. I have an array built from some MySQL data: - $all_products[1] = array( "title"=>"widget", "product_code"=>"wid-123", "colour"=>"blue"); $all_products[2] = array( "title"=>"thingy", "product_code"=>"thi-345", "colour"=>"red"); This works fine when used like: - echo 'Product #1 is a ' . $all_products[1]['colour'] . $all_products[1]['title'] . ', code: ' . $all_products[1]['code']; Gives: "Product #1 is a blue widget, code: wid-123" However, if I do this: - function showproductinfo($thisproduct) { echo 'Product #' . $thisproduct . ' is a ' . $all_products[$thisproduct]['colour'] . $all_products[$thisproduct]['title'] . ', code: ' . $all_products[$thisproduct]['code']; } showproductinfo(1); It gives nothing Can I use a variable passed via a function as the array key in this way? If so, how? And if not, what's the best way to do what I, trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/227870-data-from-array-inside-a-function-help/ Share on other sites More sharing options...
Jessica Posted February 16, 2011 Share Posted February 16, 2011 Read about variable scope. The problem is the array does not exist within that function. Quote Link to comment https://forums.phpfreaks.com/topic/227870-data-from-array-inside-a-function-help/#findComment-1175003 Share on other sites More sharing options...
ManiacDan Posted February 16, 2011 Share Posted February 16, 2011 More specifically, $all_products doesn't exist inside that function. You must pass ANYTHING you wish to use in a function into that function from the outside. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/227870-data-from-array-inside-a-function-help/#findComment-1175008 Share on other sites More sharing options...
John_A Posted February 16, 2011 Author Share Posted February 16, 2011 Thanks both for your help! I did this: - function showproductinfo($thisproduct){ global $all_products; echo 'Product #' . $thisproduct . ' is a ' . $all_products[$thisproduct]['colour'] . $all_products[$thisproduct]['title'] . ', code: ' . $all_products[$thisproduct]['code']; } showproductinfo(1); Which seems to work. Are there any downsides to this? Quote Link to comment https://forums.phpfreaks.com/topic/227870-data-from-array-inside-a-function-help/#findComment-1175015 Share on other sites More sharing options...
BlueSkyIS Posted February 16, 2011 Share Posted February 16, 2011 you should not use globals inside a function. pass the array to the function instead. You must pass ANYTHING you wish to use in a function into that function from the outside. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/227870-data-from-array-inside-a-function-help/#findComment-1175023 Share on other sites More sharing options...
John_A Posted February 16, 2011 Author Share Posted February 16, 2011 pass the array to the function instead. Every time? I'm calling the function 50 times or more on the same page. Quote Link to comment https://forums.phpfreaks.com/topic/227870-data-from-array-inside-a-function-help/#findComment-1175046 Share on other sites More sharing options...
Jessica Posted February 16, 2011 Share Posted February 16, 2011 pass the array to the function instead. Every time? I'm calling the function 50 times or more on the same page. Then you're not designing your code efficiently. Quote Link to comment https://forums.phpfreaks.com/topic/227870-data-from-array-inside-a-function-help/#findComment-1175050 Share on other sites More sharing options...
John_A Posted February 16, 2011 Author Share Posted February 16, 2011 Then you're not designing your code efficiently. It's not perfect, but it works. All I'm asking is, is it better to declare the global array variable within the function, or to pass the array to the function? Quote Link to comment https://forums.phpfreaks.com/topic/227870-data-from-array-inside-a-function-help/#findComment-1175051 Share on other sites More sharing options...
sasa Posted February 16, 2011 Share Posted February 16, 2011 beter pass index and part of array Quote Link to comment https://forums.phpfreaks.com/topic/227870-data-from-array-inside-a-function-help/#findComment-1175110 Share on other sites More sharing options...
ManiacDan Posted February 16, 2011 Share Posted February 16, 2011 If $all_products[$thisproduct] contains the product ID, just pass that. If $all_products[$thisproduct] does not contain the product ID, pass the product ID along with $all_products[$thisproduct]. Do not pass the whole array every time if all you're going to be doing is sucking out a single product. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/227870-data-from-array-inside-a-function-help/#findComment-1175256 Share on other sites More sharing options...
John_A Posted February 17, 2011 Author Share Posted February 17, 2011 beter pass index and part of array If $all_products[$thisproduct] contains the product ID, just pass that. If $all_products[$thisproduct] does not contain the product ID, pass the product ID along with $all_products[$thisproduct]. Do not pass the whole array every time if all you're going to be doing is sucking out a single product. Thanks for your help! I ended up with this which works great: - function showproductinfo($thisproduct){ echo 'Product #' . $thisproduct['product_id'] . ' is a ' . $thisproduct['colour'] . $thisproduct['title'] . ', code: ' . $thisproduct['code']; } showproductinfo($all_products[1]); Quote Link to comment https://forums.phpfreaks.com/topic/227870-data-from-array-inside-a-function-help/#findComment-1175537 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.