Jump to content

Having issues with inserting data into array inside loop


wolfcry

Recommended Posts

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.

Link to comment
Share on other sites

seems that thorpe provided the response that i was going to,  :P

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.

Link to comment
Share on other sites

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!

 

 

Link to comment
Share on other sites

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  :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.