PHP/Data Structure/implode
Содержание
- 1 Consider the following block of code
- 2 implode an array to form a string.
- 3 implode( ) function converts an array into a string by inserting a separator between each element
- 4 implode.php
- 5 Making a string from an array with implode()
- 6 Outputting an Array As a String
- 7 Printing HTML table rows with implode()
- 8 Repackage the contents of an array into a delimited string,
- 9 Using the implode() Function
Consider the following block of code
<source lang="html4strict">
string implode ( string glue, array pieces ) array explode ( string separator, string string [, int limit] )
<?php //Break the string into an array. $expdate = explode ("-","1979-06-23"); echo $expdate[0] . ","; //Then pull it back together into a string. $fulldate = implode ("-", $expdate); echo $fulldate; ?>
</source>
implode an array to form a string.
<source lang="html4strict">
Its syntax is: string implode (string delimiter, array pieces) <? $ohio_cities = array("C", "Y", "C", "C"); $city_string = implode("|", $ohio_cities); print $city_string; ?>
</source>
implode( ) function converts an array into a string by inserting a separator between each element
<source lang="html4strict">
//string implode ( string separator, array pieces ) <?
$oz = "Lions and Tigers and Bears"; $oz_array = explode(" and ", $oz); print_r($oz_array); $exclams = implode("! ", $oz_array); print_r($exclams);
?>
</source>
implode.php
<source lang="html4strict">
<?php
$cities = array("Columbus", "Akron", "Cleveland", "Cincinnati"); echo implode("|", $cities);
?>
</source>
Making a string from an array with implode()
<source lang="html4strict">
<? $dimsum = array("A","B","C"); $menu = implode(", ", $dimsum); print $menu; ?>
</source>
Outputting an Array As a String
<source lang="html4strict">
<? $languages = array("German", "French", "Spanish");
printf("Languages: %s.
\n", implode(", ", $languages));?>
</source>
Printing HTML table rows with implode()
<source lang="html4strict">
<? $dimsum = array("A","B","C"); print "<tr><td>" . implode("</td><td>",$dimsum) . "</td></tr>"; ?>
</source>
Repackage the contents of an array into a delimited string,
<source lang="html4strict">
<?php $fulldate = implode ("-", $expdate); echo $fulldate; ?>
</source>
Using the implode() Function
<source lang="html4strict">
<?php
$input_array = array("J", "M", "M"); $orig_string = implode(", ", $input_array);
?>
</source>