Page 1 of 1
Convert an Array into a string
Posted: Tue Nov 21, 2006 7:47 am
by JimiH
Hi I have the following Array, $Project_members
Code: Select all
if ( $update_project_members == 'on' ) {
$Project_Members=$_POST["Project_Members_Select"];
for ( $ii = 0 ; $ii < count($Project_Members) ; $ii++ )
{
$choose_branch = $choose_branch . " " . $Project_Members[$ii] ;
}
//$result = "You selected : " . $choose_branch ;
$Project_Members = $choose_branch;
} else {
$Project_Members=$_POST["Project_Members"];
}
I would like seperate this Array and place it in a string seperated by ";"
Example Array (bill , ben)
Would come out "bill;ben;"
Thanks
Geoff
Example
Array
BILL
Posted: Tue Nov 21, 2006 7:51 am
by feyd
Posted: Tue Nov 21, 2006 8:18 am
by JimiH
Thanks
I've been looking at that function, cant get it to work
If I echo
I get
BILL BENN
How do I put "$choose_branch" into the implode() code?
This is what I have
$result = implode (";",$choose_branch);
echo $result;
Code: Select all
This returns nothing.
Thanks
Geoff
Posted: Tue Nov 21, 2006 8:26 am
by John Cartwright
what does
Code: Select all
echo '<pre>';
print_r($choose_branch);
echo '</pre>';
$result = implode (";",$choose_branch);
echo $result;
yield?
Posted: Tue Nov 21, 2006 8:41 am
by JimiH
BILL BEN
With
Code: Select all
echo '<pre>';
print_r($choose_branch);
echo '</pre>';
$result = implode (";",$choose_branch);
echo $result;
if I comment the following line the result is the same
Code: Select all
echo '<pre>';
print_r($choose_branch);
echo '</pre>';
//$result = implode (";",$choose_branch);
//echo $result;
??
Thanks
Geoff
Posted: Tue Nov 21, 2006 8:45 am
by JayBird
$choose_branch isn't an array!?
This should work, although it is only a quick fix.
Code: Select all
$names = explode(" ", $choose_branch);
$result = implode (";",$names);
echo $result;
Posted: Tue Nov 21, 2006 8:46 am
by John Cartwright
Okay I see the error, you are overwriting your variable $choose_branch every iteration.
Code: Select all
$choose_branch = array();
for ( $ii = 0 ; $ii < count($Project_Members) ; $ii++ )
{
$choose_branch[] = $choose_branch . " " . $Project_Members[$ii] ;
}
Posted: Tue Nov 21, 2006 8:48 am
by JayBird
infact, what i think you are trying to acchieve is this
Code: Select all
if ( $update_project_members == 'on' )
{
$Project_Members=$_POST["Project_Members_Select"];
for ( $ii = 0 ; $ii < count($Project_Members) ; $ii++ )
{
$choose_branch[] = $Project_Members[$ii] ;
}
$result = implode(";", $choose_branch);
//$result = "You selected : " . $choose_branch ;
$Project_Members = $choose_branch;
}
else
{
$Project_Members=$_POST["Project_Members"];
}
Posted: Tue Nov 21, 2006 9:10 am
by JimiH
Hi thanks for all your help
The below works fine
Code: Select all
if ( $update_project_members == 'on' ) {
$Project_Members=$_POST["Project_Members_Select"];
for ( $ii = 0 ; $ii < count($Project_Members) ; $ii++ )
{
$choose_branch = $choose_branch . " " . $Project_Members[$ii] ;
}
$result = implode(";",$Project_Members);
$Project_Members = $choose_branch;
} else {
$Project_Members=$_POST["Project_Members"];
}
echo $result;
As you can see I replaced
Code: Select all
$result = implode(";",$choose_branch);
with
Code: Select all
$result = implode(";",$Project_Members);
This worked fine, can someone tell me why?
Thanks
Geoff
Posted: Tue Nov 21, 2006 9:18 am
by JayBird
JimiH wrote:
This worked fine, can someone tell me why?
Becuase $choose_branch wasn't an array
Posted: Tue Nov 21, 2006 4:34 pm
by JimiH
Thanks
I understand now
Geoff