Convert an Array into a string

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
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Convert an Array into a string

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

Thanks

I've been looking at that function, cant get it to work

If I echo

Code: Select all

echo $choose_branch;
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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

what does

Code: Select all

echo '<pre>';
print_r($choose_branch);
echo '</pre>';

$result = implode (";",$choose_branch);
echo $result;
yield?
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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;
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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] ;
}
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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"];
}
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

JimiH wrote:
This worked fine, can someone tell me why?
Becuase $choose_branch wasn't an array
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

Thanks

I understand now

Geoff
Post Reply