PHP/Data Structure/implode

Материал из Web эксперт
Версия от 07:02, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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);
?>