Jump to content

[SOLVED] PHP LOop create a long Array


abch624

Recommended Posts

Hi guys I have a field in the database that stores details of where users have visited. In the database this is stored as a long string for instance "France|Sweden|United Kingdom|United States|". The field is called "countries".

 

I want to retrive this field for all users and create a long array with all these countries.... Now what I mean is this If there are two users whose countries field in the database is "France|Sweden|United Kingdom|United States|" and "India|South Africa|United Kingdom|United States|" I would like the array to be something like this: (France, Sweden, United Kingdom, United States, France, Sweden, United Kingdom, United States,)

 

Now the code I have is something like this

 

$query = "SELECT countries AS tag
  FROM profiledetails";

$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) {
$country = $row['tag']; 
$countries = explode('|', $country);
$count = "";
foreach ($countries as $countnew) {
	$count = $count.", ".$countnew;
}
}

 

Now what happens in this is that the last entry from the database is stored. But I would like all these entries to add up as an array as I mentioned b4.

 

Pls Help guys

 

Link to comment
https://forums.phpfreaks.com/topic/113478-solved-php-loop-create-a-long-array/
Share on other sites

Thanks but that was not how...

 

I figured it out anyways thanks a lot

 

The new code is like this now

 

$query = "SELECT countries AS tag
  FROM profiledetails";

$result = mysql_query($query);
$count = array();
while ($row = mysql_fetch_array($result)) {
$country = $row['tag']; 
$countries = explode('|', $country);
$count = array_merge($countries, $count);
}

 

so basically merging arrays.....

thanks

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.