PHP/Data Structure/implode

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

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>