[SOLVED] Foreach statement

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
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

[SOLVED] Foreach statement

Post by dream2rule »

Code: Select all

<?php
$column_names = array("user_name" => "abcd","password" => "abc123#","email_id" => "abcd@yahoo.com","location" => "india");
$count = count($column_names);
echo $count."<br>";

foreach($column_names as $key => $value)
{
	echo "<br>"."$key => $value\n"."<br>";
	
}
?>
In the above code, i want to have a variable string that would contain all the keys together separated by a comma and another string containing all the values together separated by a comma.

i.e,

Code: Select all

$str_1 = "user_name, password, email_id, location";
and 
$str_2 = " abcd, abc123#, abcd@yahoo.com, india";
How should i proceed?

Thanks and Regards,
dream2rule
Last edited by dream2rule on Thu Aug 02, 2007 5:11 am, edited 1 time in total.
danf_1979
Forum Commoner
Posts: 72
Joined: Sun Feb 20, 2005 9:46 pm

Post by danf_1979 »

Maybe implode function is what you need.
http://cl2.php.net/implode
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

Last edited by Gente on Thu Aug 02, 2007 7:44 am, edited 1 time in total.
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Post by dream2rule »

thanks all i have achieved the solution finally.
Post Reply