How to use the implode function in PHP

In PHP, the implode() method joins array elements and outputs them as a single string. This is useful when we need to create one string out of a set of array values.

The function can be invoked in two ways:

  1. implode( string $glue , array $pieces ) The glue is used to combine the array pieces.
  2. implode( array $pieces )  No glue is used, so the pieces will be concatenated together.

We’ll go through examples of both, as well as how to go from a string to an array, below.

 

Example: Using implode to Go from an Array to a String

To output an array as a string, where each array value is separated by a comma and a space, we will do the following:

$list = array( 'foo', 'bar', 'baz' );                                                                                                 echo implode( ", ", $list );                                                                                                                        // outputs: foo, bar, baz

Without the previous glue string comma, this would be the result:

$list = array( 'foo', 'bar', 'baz' );                                                                                           echo implode( $list );                                                                                                                              // outputs: foobarbaz

Other functions that prove useful for looking at the values of arrays are print_r and var_dump. These two functions can be extremely useful when we are debugging a program and want to see the output of an array (or any variable).

 

How To Go From a String to an Array (The Reverse of Implode):

The reverse action going from a string to an array can be done easily with explode, or by using the preg_split function, which takes a regular expression.

 

Using explode

Explode is the opposite of implode and uses a delimiter to decide where to break up a string. It is usually the simplest way to break up a string into an array.

$keywords = explode( ', ', 'foo, bar, baz' );                                                                                    print_r( $keywords );

The above would result in the output below:

(                                                                                                                                           [0] => foo                                                                                                                                  [1] => bar                                                                                                                                 [2] => baz                                                                                                                                         )

 

Using preg_split

If delimiter is REALLY complicated, it might make sense to use a regular expression to define it. If this is the case, we can use preg_split.

$keywords = preg_split( "/[,]+/", 'foo,bar,baz' );                                                                               print_r( $keywords );

This would also output the following:

(                                                                                                                                           [0] => foo                                                                                                                                 [1] => bar                                                                                                                                 [2] => baz                                                                                                                                         )

Okay, that is enough PHP string manipulation for now. Just don’t be afraid to implode!



Was this article helpful?

Top Related Jobs

Top Freelance jobs are waiting for you, View & apply to earn more.

  • Python Developer

    $2,000, Fixed | India
    We are looking for brilliant Developer with a good English , dedicated to the projects and experienced with python,Django,php,Linux . Any additional s...
  • CorelDraw Automation

    $300, Fixed | Jordan
    We are looking to automate certain functions in CorelDraw.Details will be provided on the interview
  • Need more full-time developers to help with small projects.

    $500, Fixed | Romania
    We are looking to hire 10 new developers. The projects we will give you guys will range from $15-$500. It may be a simple 10-minute project like fixin...
  • Scripts & Utilities

    $60, Fixed | Romania
    We are running arpReach autoresponder script on our site. It was working fine with no problems and then we had to move to a new web host. After the mo...
  • Developer needed For a Cutom Portal development.

    $20, Fixed | Romania
    We need to design and integrate a new portal in our sub domain to a manage our team work. Portal will be used to assign work to team and submission fr...
  • Magneto 2 Extension Development

    $2,100, Fixed | Lithuania
    We require a Magento 2 extension based on an API, The Extension would consist of three parts. Catalog ServiceThird Party Viewer ServiceOrder Managemen...
  • PHP developer wanted for Implementation of Bitcoin Receive API

    $100, Fixed | Luxembourg
    We require a PHP developer to implement block.io API or blockcypher.com to enable us receive Bitcoin payment on our website. Any intending developer s...
  • Factory Outlet E-Commerce Project

    $14, Hourly | United Kingdom
    We seeking for dedicated Agency/Professional to work with us for a long term projectBeing able to work fast and efficiency, time is money, and we hate...


People also searched for

How to post a Job & Hire a great WordPress developer
How to post a Job & Hire a great WordPress develop...
Web Development

WordPress is an open source Content Management System (CMS), which allows the users to build dynamic websites and blog. WordPress is the most popular blogging system on the web and...

Read More
Some Useful JavaScript Tips,Tricks and Best Practices
Some Useful JavaScript Tips,Tricks and Best Practi...
Web Development

As you know, JavaScript is the top programming language in the world, the language of the web, of mobile hybrid apps (like PhoneGap or Appcelerator), of the server side (like NodeJ...

Read More
How to Write a Job Description to Find a Good PHP Developer?
How to Write a Job Description to Find a Good PHP...
Web Development

PHP (recursive acronym for PHP: Hypertext Pre-processor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be em...

Read More

You are freelancing Ninja?

Try Auto proposal today, The new virtual assistant for your freelancing world.