PHP/Data Structure/implode
Версия от 10:37, 26 мая 2010; (обсуждение)
Содержание
- 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
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;
?>
implode an array to form a string.
Its syntax is: string implode (string delimiter, array pieces)
<?
$ohio_cities = array("C", "Y", "C", "C");
$city_string = implode("|", $ohio_cities);
print $city_string;
?>
implode( ) function converts an array into a string by inserting a separator between each element
//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);
?>
implode.php
<?php
$cities = array("Columbus", "Akron", "Cleveland", "Cincinnati");
echo implode("|", $cities);
?>
Making a string from an array with implode()
<?
$dimsum = array("A","B","C");
$menu = implode(", ", $dimsum);
print $menu;
?>
Outputting an Array As a String
<?
$languages = array("German", "French", "Spanish");
printf("<p>Languages: %s.</p>\n", implode(", ", $languages));
?>
Printing HTML table rows with implode()
<?
$dimsum = array("A","B","C");
print "<tr><td>" . implode("</td><td>",$dimsum) . "</td></tr>";
?>
Repackage the contents of an array into a delimited string,
<?php
$fulldate = implode ("-", $expdate);
echo $fulldate;
?>
Using the implode() Function
<?php
$input_array = array("J",
"M",
"M");
$orig_string = implode(", ", $input_array);
?>