Jump to content

convert array to string in php


rishe

Recommended Posts

i am fetching data from database. and storing that data in the array.

the result is

 

Array ( [0] => Array ( [workspace] => cite [layer1] => layer [layer2] => atm layer1 [layer3] => atm layer2 ) )

 

how to convert the above array as string.

so that the result should be like this cite,layer , atm layer1 ,atm layer2

 

my code is

 

$dept= "ATM";

//Create the query
$sql = "SELECT `workspace`,`layer1`,`layer2`,`layer3` FROM `dept_desc` where `departments` = '$dept'";
//Run the query
$result = mysql_query($sql);
if (! $result){
throw new My_Db_Exception('Database error: ' . mysql_error());
}
$arr = array();
while($row = mysql_fetch_assoc($result))
{
$arr[] = $row;
}
print_r ($arr);
Link to comment
Share on other sites

There's the implode() function. I have not tested on an associative array, but it likely works as well. In the case that it doesn't, just loop through the items with a foreach() loop and append them to a string.

  • Like 1
Link to comment
Share on other sites

i have tried with implode() function, but i am getting error as "Warning: implode(): Invalid arguments".

 

My code is

$dept= "ATM";

 

$sql = "SELECT `layer1`,`layer2`,`layer3` FROM `dept_desc` where `departments` = '$dept'";

//Run the query
$result = mysql_query($sql);
if (! $result){
throw new My_Db_Exception('Database error: ' . mysql_error());
}
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$layers=
"['{$row['layer1']}' ,".
"'{$row['layer2']}', ".
"'{$row['layer3']}' ]".
"<br>";
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...