Brandrally
Brandrally

Reputation: 851

Formatting php array output

I need a little help with my PHP. Basically I have a field a page that I need the code to produce an outcome that looks like:

$existing_users=array('bra','vis','rfm'); 

Where it pulls the 'bra', 'vis', 'rfm' from the database.

So I figured cool lets make this into an array, and then explode it / separate it with '', Which I am not sure is the best approach, though I guess that's where my logic took me. And so I created the array

mysql_select_db($database_db, $db);
$result = mysql_query('SELECT * FROM clients') or exit(mysql_error());
while ($row = mysql_fetch_assoc($result)) {$array[] = $row['shortcode'];}

print_r($array);  

Which gives me this:

Array
(
    [0] => bra
    [1] => vis
    [2] => rfm
)

This is where I am stuck. I am not quite sure how to make things happen to my desired result. If anyone can provide me with advice I would be really appreciative.

Upvotes: 0

Views: 93

Answers (3)

Kalaiyarasan
Kalaiyarasan

Reputation: 13374

You can declare the array before the while loop and you can push the values into it like

$existing_users = array();
mysql_select_db($database_db, $db);
$result = mysql_query('SELECT * FROM clients') or exit(mysql_error());
while ($row = mysql_fetch_assoc($result)) {$existing_users[] = $row['shortcode'];}

print_r($existing_users);

Upvotes: 4

Shomz
Shomz

Reputation: 37701

foreach($array as $user){
    $existing_users[] = $user;
}

Upvotes: 1

MimiEAM
MimiEAM

Reputation: 2630

have you tried this ?

$existing_users=array($array[0],$array[1],$array[2]); 

a raw way of doing what you want , you could use a loop and push the element on the new array if the return of your query is too big

Upvotes: 1

Related Questions