|
|
 |
implode (PHP 3, PHP 4 , PHP 5) implode -- Join array elements with a string Descriptionstring 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.
例子 1. implode() example |
<?php
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone
?>
|
|
注:
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.
注:
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.
See also explode(), and split().
php at woodenpickle dot com
15-Mar-2002 02:55
Hey, I found a good use for implode() today. It came from a need to have a
<select name=state multiple> box on the page that a person could
select multiple states from and send those to a >SELECT * FROM customers
WHERE state='$state'; query. But they need to be able to send just one
state also. Well, I changes the html select box to say <select
name=state[] multiple>. This turns $state into an array as you may know.
I then, later on in the script, did this:
$count = count( $state
); if( $count > 1 ) { $states = implode( "'.'",
$state ); $result = $database->query( "SELECT * FROM
currentOrders WHERE date>=$from AND date <=$to AND state IN
('$states')" ); }
//This takes care of multiple states, but
if the user sent only one state, I catch it here:
foreach( $state as
$value ); if( $value ) $result = $database->query(
"SELECT * FROM currentOrders WHERE date>=$from AND date<=$to AND
state='$value'" ); else //This one will catch the ALL states option
if they choose that. $result = $database->query( "SELECT *
FROM currentOrders WHERE date>=$from AND date<=$to"
);
Anyway, I thought I'd post this up here in case it might help
someone. Or maybe someone could figure out a better way and enlighten us
all.. Have fun.. Bob
| |