Home > cakePhp, javascript, php, Software Engineering, Web, wordpress > Array to String Conversion using php

Array to String Conversion using php

it normally happen you are convert  some delimited string to array and php do it as using “explode()”

array explode ( string $delimiter , string $string [, int $limit ] )

php also provide the facility to convert array to string and most commonly used method is “implode”

string implode ( string $glue , array $pieces )

e.g lets we have an array of student names and we want to print 

<?php

$students = array( ‘khalid’, ‘Umar’, ‘mazhar’,’junaid’ );
$comma_separated = implode(“,”, $students);

echo $comma_separated; // ‘khalid’, ‘Umar’, ‘mazhar’,’junaid’

// Empty string when using an empty array:
var_dump(implode(‘hello’, array())); // string(0) “”

?>

Advertisements
  1. mahendra
    June 2, 2011 at 7:50 am

    It’s a good solution. I got an answer and i solved my problem using implod. Here is an example of this – > $service = array(“Eseekho”,”Sparsh”,”Cstories”);
    $service = implode(“,”,$service);
    $product = explode(“,”,$service);
    <option value="”>

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: