wolfcry Posted December 31, 2011 Share Posted December 31, 2011 Hello all, What I'm trying to do is insert dynamic data supplied by the user into an array, but for some reason, the loop only builds arrays inside of arrays (or so it seems) and doesn't create an array with unique indexes and associated data. I was hoping someone could point me in the right path? I've searched numerous forums, threads and google but cannot find an answer. Here is the code snippet I'm working on. What it's supposed to do is collect a user's input, which is a Quantity amount and a high number, and based on that amount, create an array of random integers. This is my latest attempt. Now, maybe I'm just losing it, but when I place print_r($output) inside the loop, I see how it's being created but no unique indexes, just nested arrays. When I place it outside of the loop, I only receive one array with one index and value even though a high quantity was sent to the script. $i = 1; while ($i <= $Quantity){ $output = array(rand($RandLow, $RandHigh)); $i++; } print_r($output); Here is another attempt, which again proved to be unsuccessful, at least to my tire brain. $i = 1; while ($i <= $Quantity){ $output = rand($RandLow, $RandHigh); $total = array($output); $i++; } print_r($total); I've tried the for() loop as well but I just can't seem to make heads or tails of this and I'm sure it's going to be something simple that I'm missing. Thanks in advance for any help and insight if you know a better way to do this. Quote Link to comment https://forums.phpfreaks.com/topic/254104-having-issues-with-inserting-data-into-array-inside-loop/ Share on other sites More sharing options...
trq Posted December 31, 2011 Share Posted December 31, 2011 $i = 1; $output = array(); while ($i <= $Quantity){ $output[] = rand($RandLow, $RandHigh); $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/254104-having-issues-with-inserting-data-into-array-inside-loop/#findComment-1302734 Share on other sites More sharing options...
AyKay47 Posted December 31, 2011 Share Posted December 31, 2011 seems that thorpe provided the response that i was going to, so I will explain what went wrong. The code that you have, $i = 1; while ($i <= $Quantity){ $output = array(rand($RandLow, $RandHigh)); $i++; } print_r($output); rewrites your $ouput variable to an array with only one value every iteration.. what needs to be done, as thorpe illustrated, is instead of declaring your array inside of your loop, which will result in the array being rewritten every iteration, you need to declare an empty array before your loop and then add to the array inside of your loop. Quote Link to comment https://forums.phpfreaks.com/topic/254104-having-issues-with-inserting-data-into-array-inside-loop/#findComment-1302735 Share on other sites More sharing options...
wolfcry Posted December 31, 2011 Author Share Posted December 31, 2011 Hi AyKay and Thorpe, When I saw what you two posted, I literally did this => *groan and face palm* I'm now hanging my head in shame lol. I completely spaced placing the array outside of the loop. Thank you both for your help, and the concise explanation. They both were definitely appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/254104-having-issues-with-inserting-data-into-array-inside-loop/#findComment-1302736 Share on other sites More sharing options...
laffin Posted December 31, 2011 Share Posted December 31, 2011 And here I thought you were trying to do something a lot more complicated as actual inserting elements into an array. Well good luck Quote Link to comment https://forums.phpfreaks.com/topic/254104-having-issues-with-inserting-data-into-array-inside-loop/#findComment-1302755 Share on other sites More sharing options...
wolfcry Posted December 31, 2011 Author Share Posted December 31, 2011 Hey laffin, Actually the problem was me not being able to insert the elements and the above code is a snippet from a program that requires that information, but like it does most of the time, my brain malfunctioned and I was having the dickens of a time trying to resolve the issue. I think I suffer from early alzheimers at times Quote Link to comment https://forums.phpfreaks.com/topic/254104-having-issues-with-inserting-data-into-array-inside-loop/#findComment-1302789 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.