Jump to content

Unknown values in Array


ILikeBread

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/128167-unknown-values-in-array/
Share on other sites

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.