DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   PHP (http://www.daniweb.com/forums/forum17.html)
-   -   Initialization of an Array (http://www.daniweb.com/forums/thread123335.html)

OmniX May 9th, 2008 4:38 am
Initialization of an Array
 
I use an array for the first time in a while loop without declaring it(as php does not need it)
  1. $variable_array[] = $fetch_array['column'];

As the while loop is repeating it increases the number of the array.
Am I doing this right?

Should I have before the loop some code like:
  1. $variable_array = array();

Dont know abit confused and lost.

Thanks, Regards X

nav33n May 9th, 2008 4:48 am
Re: Initialization of an Array
 
As you have already said it yourself, you don't have to explicitly declare a variable as an array to use it like an array. You can simply use $var[] = "value"; This will treat $var as an array. But, its always a good practice to initialize a variable before using it. (But most of them don't do it ;) )

OmniX May 9th, 2008 5:01 am
Re: Initialization of an Array
 
Nav to the rescue again!

Umm nav I can declare it but I cant initialize it as I dont want to give it a value till used in the while loop, so.

What code would I use?

$var[];

?

nav33n May 9th, 2008 5:21 am
Re: Initialization of an Array
 
  1. <?php
  2. $var = array( ); //declare an array
  3. $query = "select * from table";
  4. $result = mysql_query($query);
  5. while($row = mysql_fetch_array($result)) {
  6.   $var[] = $row['name']; //assign a value
  7. }
  8. ?>
Note: The code works even without declaring a variable as array.
But, you can't use a normal variable to act like an array. Example,
  1. <?php
  2. $x = 100;
  3. $x[] = "10";
  4. print $x;
  5. ?>

OmniX May 9th, 2008 6:18 am
Re: Initialization of an Array
 
Ya nav that what I have, but I cant seem to print the $var variable should just be print/print_r/echo $var; but none of it works...

nav33n May 9th, 2008 6:26 am
Re: Initialization of an Array
 
Why ? print_r($arrayname) should work fine.

OmniX May 9th, 2008 7:25 am
Re: Initialization of an Array
 
Its being stupid, in short.

It wont let me declare the array before the html, I thought it was possible?

Anyways I have found another way to do it, just need a questions answered.

[code syntax="php"]
while($row = mysql_fetch_array($result)) {
//then to release after it is used so you can reuse it is?
mysql_free_result($result);
[/code]

This correct, Thanks.

nav33n May 9th, 2008 7:35 am
Re: Initialization of an Array
 
When you execute a query, a resource identifier will be assigned to $result. mysql_free_result will free this value.

OmniX May 9th, 2008 8:20 am
Re: Initialization of an Array
 
Allowing me to reuse the query again or in other words fetch it again?

Man nav php can be so annoying at times I fix one error and a new error pops up. I throw the arrays in a forloop and now more errors :( . Ill get back to debuging and keep you posted to see if anything works.

PS: can you run queries in a for loop? That could be a possible error? or declare it differently?

nav33n May 9th, 2008 8:31 am
Re: Initialization of an Array
 
I don't know whats causing the error. You can run queries in a for loop. Once you have freed the result using mysql_free_result, that resource identifier is freed. Actually, if your query isn't too complicated, you don't have to use free_result as php frees the result at the end of execution of the script. Source: http://nl3.php.net/mysql_free_result

Can you post your code here ?


All times are GMT -4. The time now is 5:25 am.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC