[Solved] Dynamically populating array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

[Solved] Dynamically populating array

Post by Addos »

I need to dynamically add content to an array I’m using so for example I’m using the following:

Code: Select all

$review_text = array("I", "Ate", "My", "Dinner" );
But I want to dynamically populate this from fields in my database and I think I need to run some kind of loop.

For example how can I pass this which returns i Ate My Dinner

Code: Select all

do {
 
$text = $row_GetRandomReview['random_details_field'];
 
} while ($row_GetRandomReview1 = mysql_fetch_assoc($GetRandomReview1));
Into my array e.g. similar to something like this:

Code: Select all

$review_text = array($text );
Thanks for any help.
Last edited by Addos on Thu Oct 23, 2008 6:24 am, edited 1 time in total.
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: Dynimically populating array

Post by watson516 »

Appending the new stuff to the end of the array

Code: Select all

 
$someArray=array();
$someArray[]="New Item";
 
Just put that in your do loop, minus the initialization of the array.
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Re: Dynimically populating array

Post by dude81 »

Change your code as below


Code: Select all

 
$text = array()
do {
 
$text[] = $row_GetRandomReview['random_details_field'];
 
} while ($row_GetRandomReview1 = mysql_fetch_assoc($GetRandomReview1));
Into my array e.g. similar to something like this:

Code: Select all

$review_text = $text
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Re: Dynimically populating array

Post by Addos »

Thank you both I got this to work a treat!
:wink:
Post Reply