ILikeBread Posted October 13, 2008 Share Posted October 13, 2008 Hi Hopefully someone can help me. Using the example below $NumberArray= array(1,2,3,4,5,6); If i didn't know how many values i wanted to add until i recieved information from the database at page load, how would i program an array in this format? In the above example i know i need 6 places in the array. But i want something like $NumberArray= array(VARIABLE NUMBER OF SLOTS); To save people showing me this as an answer "$NumberArray[n]" that is not what i am after. I was thinking something like this array( for ($i = 0; $i <= $ID; $i++) { $NumberArray); } ); But it doesnt work Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/128167-unknown-values-in-array/ Share on other sites More sharing options...
Zane Posted October 13, 2008 Share Posted October 13, 2008 unlike other programming languages..in PHP you don't have to declare an array's length before using it if that's what you're really asking. but usually to populate an array with the results of a mysql result it looks like this $row = null; //Row begins as nothing //Also, you don't have to declare this either I just put it for demonstration while($row = mysql_fetch_array()) { echo $row['name']; } //Now row is an array of....I really don't know what size prints the Names of all that are in the results Quote Link to comment https://forums.phpfreaks.com/topic/128167-unknown-values-in-array/#findComment-663807 Share on other sites More sharing options...
ILikeBread Posted October 13, 2008 Author Share Posted October 13, 2008 Thats sort of what i am after but also not quite it. I'll go into more detail. I currently use your exact script to pull my data out of my MySQL database. I then filter the data into an array named "$Profit[n]" "$Profit[n]" changes how many things go into it on almost a daily basis. Lets say Profit[n] looks like this Profit[0] = 2; Profit[1] = 4; Profit[2] = 6; I then want to be able to add it into the format array("ALL PROFIT RECORDS"); (using the example above) array(Profit[0], Profit[1], Profit[2]); My main problem is that I can't seem to replicate the Profit[n] in the new array at run time. I realize i am replicating data but please humor me. Quote Link to comment https://forums.phpfreaks.com/topic/128167-unknown-values-in-array/#findComment-663815 Share on other sites More sharing options...
Zane Posted October 13, 2008 Share Posted October 13, 2008 So........you're trying to put an array into an array? what's wrong with doing this $allProfits[] = $Profit[0]; $allProfits[] = $Profit[1]; $allProfits[] = $Profit[2]; $allProfits[] = $Profit[3]; Quote Link to comment https://forums.phpfreaks.com/topic/128167-unknown-values-in-array/#findComment-663820 Share on other sites More sharing options...
Jibberish Posted October 13, 2008 Share Posted October 13, 2008 <?php foreach ($profit as $key => $value) { $newArray[$key] = $value; } ?> would do that, it would use the keys from the current array as the new keys in this one. but its pretty much just making a copy of that array with a new name isnt it? Quote Link to comment https://forums.phpfreaks.com/topic/128167-unknown-values-in-array/#findComment-663822 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.