• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 113
  • Last Modified:

implode/explode

I see implode/explode used all the time!  as far as i know all they do is break up strings and put them together?  how come there used so much?  I cant think of to many uses for them. :(
0
chilled2003
Asked:
chilled2003
3 Solutions
 
xakemSoftware Application DeveloperCommented:
this functions very helpful for manipulating string parts, as for me they are sometime dates, sometime cookies or session variables...encode/decode... i can't remember all of the cases.
0
 
hernst42Commented:
as HTML does not have a datatype for arrays, you use implode to make your PHP-datastructure to fit into HTML. When you get those values from HTML you split the mim explode into a PHP-array as it is easyer to work with array than with string delimited by a special character.
0
 
chilled2003Author Commented:
anymore common uses for it?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
chilled2003Author Commented:
hmmm so implode, explode is mostly used to put data in and out of arrays?
0
 
Diablo84Commented:
Their purpose is working with arrays, explode breaks up a string into an array using a string seperator character and implode joins array items together using a string glue character.

So

Explode: string to array
Implode: array to string
0
 
Diablo84Commented:
Example

$string = "hello|there|i|am|a|string";

$parts = explode("|",$string);

Parts is now an array: Array ( [0] => hello [1] => there [2] => i [3] => am [4] => a [5] => string )

$outstring = implode(" ",$parts);

Out string joins the array items together with spaces forming the string: hello there i am a string
0
 
RoonaanCommented:
I have not much to add to the things Diableo84 said.

I often use it to implode the results from the file function. But this can all be skipped when using filegetcontents() instead of implode('', file());

I relation to session you could use implode to store the session data (of parts of it) if you are sure it are all numeric values or all string values. Then you can implode things before storing to gain a string like "1,2,3,4" and when restoring your session just explode it back into an array. But then again, you would probably use serialize.

One thing I use implode a lot, is when i have an array of strings and want to output it as table cells or list items:

echo '<tr><td>'.implode('</td><td>', $data).'</td></tr>';
echo '<li>'.implode('</li><li>', $data).'</li>';

This saves a lot of typing, and i guess there are more ways to easily output arrays of data.

-r-
0
 
chilled2003Author Commented:
Roonaan thanks for the extra info :D
http://www.experts-exchange.com/Web/Q_21135983.html
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now