Thomisback Posted January 19, 2009 Share Posted January 19, 2009 Hi, I have made a small PHP script, the problem is that after I run it the array is still empty. But if I print_r the array inside the function it does work and prints one value. <?php function test_function($parameter){ $thiz[] = "testvalue"; return(1); } $thiz = array(); $this_array = array(); $this_array[] = "value1"; $this_array[] = "value2"; foreach ($this_array as $parameter){ test_function($parameter); } print_r($thiz); ?> Kind regards Quote Link to comment https://forums.phpfreaks.com/topic/141430-solved-empty-array-array/ Share on other sites More sharing options...
cwarn23 Posted January 19, 2009 Share Posted January 19, 2009 Try the below code: <?php $thiz = array(); $this_array = array(); $this_array[] = "value1"; $this_array[] = "value2"; foreach ($this_array as $compare){ $thiz[] = "testvalue"; } print_r($thiz); ?> The reason why you couldn't retrieve the values from what I can see is that functions cannot call variable/array values from outside the function. So if you have a variable and you want to pass it to a function, you can 1, use the function brackets or 2, use sessions. But the easiest thing to do in your case was just to remove the entire function since it did not contain much. Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/141430-solved-empty-array-array/#findComment-740321 Share on other sites More sharing options...
Prismatic Posted January 19, 2009 Share Posted January 19, 2009 <?php function test_function($parameter) { global $thiz; $thiz[] = $parameter; return; } $this_array = array(); $this_array[] = "value1"; $this_array[] = "value2"; foreach ($this_array as $parameter){ test_function($parameter); } print_r($thiz); ?> Quote Link to comment https://forums.phpfreaks.com/topic/141430-solved-empty-array-array/#findComment-740324 Share on other sites More sharing options...
Thomisback Posted January 19, 2009 Author Share Posted January 19, 2009 Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/141430-solved-empty-array-array/#findComment-740325 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.