Portrety Uliczne Nieznajomych - zobacz wyjątkową galerię portretów z warszawskich ulic
ZALOGUJ SIĘ
login:
hasło:
przypomnij hasło
załóż konto użytkownika
(i zobacz kilka porad gratis)
   
WYSZUKIWARKA I DZIAŁY
całe porady  tytuły
zaznacz działy do przeszukania
(brak wyboru = wszystkie działy)
PHP
MySQL >
PostgreSQL
SQLite
Perl
Java
XML
XSLT
XPath
WML
SVG
RegExp
Wyszukiwarki
Ochrona
VBScript
Google Plus
XHTML/CSS
JavaScript
Grafika
Flash
Photoshop
Windows
Linux
Bash
Apache
Procmail
E-biznes
Explorer
Opera
Firefox
Inne porady
   
KURSY, DOKUMENTACJE
Własne:
XHTML/CSS
JavaScript
ActionScript
WML, RSS, SSI
Pozostałe:
PHP
MySQL
Java API
więcej...
   
użytkowników online: 18
W CZYM MOGĘ POMÓC?


   
OPINIE UŻYTKOWNIKÓW
Prawdziwa skarbnica wiedzy na temat tworzenia stron WWW i nie tylko. Korzystam z porad praktycznie codziennie, jest mi to niezbędne w mojej pracy. Sam zajmuję się tworzeniem serwisów, ale porady pisane przez Darka sa dla mnie nieocenioną pomocą! Proste, czytelne i zrozumiałe dla każdego! Czekam na więcej!

Krzysztof Szypulski
KESS - projektowanie stron

   
GALERIA FOTOGRAFII
   
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net

[Spis] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [X] [W] [Z]

implode

(PHP 3, PHP 4, PHP 5)

implode -- Join array elements with a string

Description

string implode ( string glue, array pieces )

Returns a string containing a string representation of all the array elements in the same order, with the glue string between each element.

Przykład 1. implode() example

<?php

$array
= array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

echo
$comma_separated; // lastname,email,phone

?>

Notatka: implode() can, for historical reasons, accept its parameters in either order. For consistency with explode(), however, it may be less confusing to use the documented order of arguments.

Notatka: As of PHP 4.3.0, the glue parameter of implode() is optional and defaults to the empty string(''). This is not the preferred usage of implode(). We recommend to always use two parameters for compatibility with older versions.

Notatka: Ta funkcja jest bezpieczna dla danych binarnych.

See also explode(), and split().




User Contributed Notes

02-Feb-2006 08:48

FYI: "As of PHP 4.3.0, the glue parameter of implode() is optional and defaults to the empty string(''). This is not the preferred usage of implode(). We recommend to always use two parameters for compatibility with older versions."

This doesn't seem to work consistently. I was getting NULL from implode with no arguments on 4.3.8.


dabduster at gmail dot com
03-Jan-2006 05:39

an implementation of adrian at foeder dot de implode_with_keys function for input and update sql statement.

function implode_param($glue, $array, $valwrap='', $mode = 0)
   {
   /*   
   if mode = 0 output is key and values
   if mode = 1 output only keys
   if mode = 2 output only values
   */
  
   switch ($mode){
       case 1:
               foreach($array AS $key => $value) {
                   $ret[] = $valwrap.$key.$valwrap;
               }
       break;
      
       case 2:
               foreach($array AS $key => $value) {
                   $ret[] = $valwrap.$value.$valwrap;
               }
       break;

       default:
       case 0:
               foreach($array AS $key => $value) {
                   $ret[] = $key."=".$valwrap.$value.$valwrap;
               }
       break;
      
   }
      
      
   return implode($glue, $ret);
}


Hotmail resident Tree2054
08-Dec-2005 08:57

Combining into one function:

function implode_with_options($assoc_array, $prefix = '', $k_v_glue = '', $vwrap = '', $seperator = '')
{
   foreach ($assoc_array as $k => $v)
   {
       $tmp[] = $k . $k_v_glue . $vwrap . $v . $vwrap;
   }
   return $prefix . implode($seperator, $tmp);
}

$items = array('Apples', 'number' => 3, 'Vehicle' => '');

// For a query string:
echo implode_with_options($items, '?', '=', '', '&');

/**
 * Produces
 * ?0=Apples&number=3&Vehicle=
 */

// For a SQL query
echo implode_with_options($items, '', '=', '"', ', ');

/**
 * Produces
 * 0="Apples", number="3", Vehicle=""
 */


adrian at foeder dot de
31-Oct-2005 01:53

...and a mysql-update-statement-compatible implementation of implode_with_keys:

<?php
function implode_with_keys($glue, $array, $valwrap='')
   {
       foreach(
$array AS $key => $value) {
          
$ret[] = $key."=".$valwrap.$value.$valwrap;
       }
       return
implode($glue, $ret);
   }
?>

so implode_with_keys(", ", $array, "'") will output:

key1='value1', key2='value2'

and so on. Useful for UPDATE table SET key1='value1', key2='value2'


Peter Hopfgartner
27-Sep-2005 04:26

Correctly initializing all variables, this would become:

function implode_with_key($assoc, $inglue = '=', $outglue = '&'){
   $return = '';
   foreach ($assoc as $tk => $tv) {
       $return = ($return != '' ? $return . $outglue : '') .
           $tk . $inglue . $tv;
   }
   return $return;
}

Note, the return value is also well defined if $assoc is empty.

Regards


php at josh dot jeppsons dot org
09-Sep-2005 06:22

Another variation on implode_with_key:

<?php
 
function implode_with_key($assoc, $inglue = '=', $outglue = '&')
   foreach (
$assoc as $tk => $tv) {
    
$return = (isset($return) ? $return . $outglue : '') . $tk . $inglue . $tv;
   }
   return
$return;
  }
?>


pr10n at spymac dot com
29-Aug-2005 06:46

A little tweak on info at urbits dot com's suggestion just incase someone changes their value of $outglue:

<?php
function implode_with_key($assoc, $inglue = '=', $outglue = '&')
{
  
$return = null;
   foreach (
$assoc as $tk => $tv) $return .= $outglue.$tk.$inglue.$tv;
   return
substr($return,strlen($outglue));
}
?>


info at urbits dot com
20-Aug-2005 03:06

I liked memandeemail's (27-Apr-2005) neat code for imploding an associative array. I have done a mod so that, by default, it returns a url query string.

<?php
function implode_with_key($assoc, $inglue = '=', $outglue = '&')
{
  
$return = null;
   foreach (
$assoc as $tk => $tv) $return .= $outglue.$tk.$inglue.$tv;
   return
substr($return,1);
}
?>

Example:
<?php
$assoc_array
= array("a" => "foo", "b" => "bar", "c" => "foobar");
echo (
implode_with_key($assoc_array);
?>

ouput: a=foo&b=bar&c=foobar

usage: After altering the $HTTP_GET_VARS values, I pass $HTTP_GET_VARS to the function to easily build variation urls for links and header redirects.

note: This function doesn't encode the url string or check for empty variables.


cristianDOTzuddas [AT] gmailDOTcom
07-Jul-2005 09:22

...and another variation of "implode_assoc" function. Just added the boolean parameter $urlencoded; if TRUE returns the array value in URL encod format. If the parameter is not given it behaves like the original function.

<?
function implode_assoc($inner_glue, $outer_glue, $array, $skip_empty=false, $urlencoded=false) {
  
$output = array();
   foreach(
$array as $key=>$item) {
       if (!
$skip_empty || isset($item)) {
           if (
$urlencoded)
              
$output[] = $key.$inner_glue.urlencode($item);
           else
              
$output[] = $key.$inner_glue.$item;
       }
   }
  
   return
implode($outer_glue, $output);
}
?>


sam dot bledsoe at nosp at nn dot gmail dot com
01-Jun-2005 05:57

The function below recursively outputs an array in a format condusive to parsing it in php or another scripting language.  It does NOT output the name of the original array, for that see note 1.  It handles all the cases I could think of elegantly.  Comments and criticisms are welcome.

For an array constructed with:

$arr = array("foo" => array('bar' => array(0 => "value 0", 1 => "value 1")), "foo two" => array(0 => array("bar" => "value2")));

The line below:

echo implode_parseable("=", ";<br>$", $arr, "$", ";");

Will produce:

$foo["bar"][0]="value 0";
$foo["bar"][1]="value 1";
$foo_two[0]["bar"]="value2";

NOTES:
1)  If the leading identifier on a line is a number, the output will most likely be unusable since variable names cannot begin with numbers.  You can get around this by doing something like:
$arr = array('arr' => $arr);
This will output the array as it actually is (because the key is the same name as the array) instead of just its fields.
2)  Since spaces are not allowed in variable names, they are replaced in lines' leading identifiers by the $space_replace_char parameter, '_' by default.

Hopefully someone will find this useful, if so drop me a line.  Credit and thanks go out to the people who posted their code on this manual page, especially davidpk212 at gmail dot com and phpWalter at torres dot ws.

function implode_parseable($inner_glue = "=", $outer_glue = "\n", $array = null, $prefix = "", $suffix = "", $space_replace_char = '_', $skip_empty = false, $current_loc = "", $recursion_level = 0){
  return $prefix . implode_parseable_r($inner_glue, $outer_glue, $array, $space_replace_char, $skip_empty, $current_loc, $recursion_level) . $suffix;
}

function implode_parseable_r($inner_glue = "=", $outer_glue = "\n", $array = null, $space_replace_char = '_', $skip_empty = false, $current_loc = "", $recursion_level = 0)
{
  if(is_array($array)){
   $output = array();
   foreach( $array as $key => $item ){
     if ( is_array ($item) ){
      
       //don't quote numeric indicies
       if(is_string($key))
         $quoted_key = "\"" . $key . "\"";
       else
         $quoted_key = $key;

       // This is value is an array, go and do it again!
       $level = $recursion_level + 1;
       if($recursion_level == 0){
         // can't have spaces in a variable name!
         $current_loc .= str_replace(' ', $space_replace_char, $key);
         $output[] = implode_parseable_r ($inner_glue, $outer_glue, $item,  '_', $skip_empty, $current_loc, $level);
         //start the position tracker over after every run from level 0
         $current_loc = '';
       }else{
         $current_loc .= "[" . $quoted_key . "]";
         $output[] = implode_parseable_r ($inner_glue, $outer_glue, $item,  '_', $skip_empty, $current_loc, $level);
         //remove the last index from the position tracker string after using it
         $current_loc = ereg_replace('\[[^]]*\]$', '', $current_loc);
       }

     }
     else{
       //  don't quote or []ify the base variable name,
       //  but do for all else as appropriate
       if($recursion_level != 0){
           if(is_string($key))
             $key = "\"" . $key . "\"";
           $key = "[" . $key . "]";
       }
//        echo "<br>";
//        var_dump($item);
//        echo "<br>";

       $skip_this = false;
       if($skip_empty && (!isset($item) || $item == NULL || $item == '')) $skip_this = true;

       //quote the item (which is the value of the array index) if it is a string
       if(is_string($item)) $item = "\"" . $item . "\"";

       if(!$skip_this) $output[] = $current_loc . $key . $inner_glue . $item;
     }
   }
   return implode($outer_glue, $output);
  }else{
   return $array;
  }
}


Kl

 

 
  © 1996-2012 & Reporter.plmiejscao serwisieabonamentwarunki korzystaniaRSSkontakt