Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #4091 > unrolled thread
| Started by | Mike <mpearl1@gmail.com> |
|---|---|
| First post | 2011-12-19 12:13 -0800 |
| Last post | 2011-12-20 02:55 +0100 |
| Articles | 10 — 4 participants |
Back to article view | Back to comp.lang.php
implode() order? Mike <mpearl1@gmail.com> - 2011-12-19 12:13 -0800
Re: implode() order? Mike <mpearl1@gmail.com> - 2011-12-19 12:15 -0800
Re: implode() order? Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-12-19 21:23 +0100
Re: implode() order? Mike <mpearl1@gmail.com> - 2011-12-19 12:36 -0800
Re: implode() order? Jerry Stuckle <jstucklex@attglobal.net> - 2011-12-19 16:15 -0500
Re: implode() order? Mike <mpearl1@gmail.com> - 2011-12-19 13:30 -0800
Re: implode() order? Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-12-19 23:47 +0100
Re: implode() order? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-12-20 02:59 +0100
Re: implode() order? Jerry Stuckle <jstucklex@attglobal.net> - 2011-12-19 22:34 -0500
Re: implode() order? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-12-20 02:55 +0100
| From | Mike <mpearl1@gmail.com> |
|---|---|
| Date | 2011-12-19 12:13 -0800 |
| Subject | implode() order? |
| Message-ID | <23705179.475.1324325630088.JavaMail.geo-discussion-forums@yqgn9> |
How can I specify the order that an Array() is imploded? See code below:
[code]
<?php
$toImplode = Array(
1 => "Second",
0 => "First",
2 => "Third"
);
$finalString = implode(" - ", $toImplode);
echo "<p>" . $finalString . "</p>";
/* Results as "Second - First - Third"
* But I need it to be "First - Second - Third"
*/
?>
[/code]
[toc] | [next] | [standalone]
| From | Mike <mpearl1@gmail.com> |
|---|---|
| Date | 2011-12-19 12:15 -0800 |
| Message-ID | <28876384.735.1324325756648.JavaMail.geo-discussion-forums@yqdj19> |
| In reply to | #4091 |
The actual code uses named keys: [code] $toImplode = Array( "b" => "Second", "a" => "First", "c" => "Third" ); [/code]
[toc] | [prev] | [next] | [standalone]
| From | Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> |
|---|---|
| Date | 2011-12-19 21:23 +0100 |
| Message-ID | <jco6gd$96e$1@news.albasani.net> |
| In reply to | #4091 |
Mike schrieb:
> How can I specify the order that an Array() is imploded? See code below:
>
> [code]
> <?php
> $toImplode = Array(
> 1 => "Second",
> 0 => "First",
> 2 => "Third"
> );
>
> $finalString = implode(" - ", $toImplode);
>
> echo "<p>" . $finalString . "</p>";
>
> /* Results as "Second - First - Third"
> * But I need it to be "First - Second - Third"
> */
> ?>
> [/code]
Do a ksort( $toImplode ); before the implode. (See
http://de.php.net/manual/en/function.ksort.php)
Greetings,
Thomas
--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
[toc] | [prev] | [next] | [standalone]
| From | Mike <mpearl1@gmail.com> |
|---|---|
| Date | 2011-12-19 12:36 -0800 |
| Message-ID | <10925269.424.1324326983924.JavaMail.geo-discussion-forums@yqnd19> |
| In reply to | #4093 |
Thanks... suppose the keys are not in a sortable order, like this: $toImplode = Array( "second" = "Second", "fourth" = "Fourth", "third" = "Third", "first" = "First" ); Or would I need to simply: echo $toImplode["first"] . $toImplode["second"]; // etc.
[toc] | [prev] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2011-12-19 16:15 -0500 |
| Message-ID | <jco9hb$ghc$1@dont-email.me> |
| In reply to | #4095 |
On 12/19/2011 3:36 PM, Mike wrote: > Thanks... suppose the keys are not in a sortable order, like this: > > $toImplode = Array( > "second" = "Second", > "fourth" = "Fourth", > "third" = "Third", > "first" = "First" > ); > > Or would I need to simply: > > echo $toImplode["first"] . $toImplode["second"]; // etc. Use usort() and define your own comparison function. Or you could just use the individual elements as you indicated. There is no rule you have to use implode(). -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ==================
[toc] | [prev] | [next] | [standalone]
| From | Mike <mpearl1@gmail.com> |
|---|---|
| Date | 2011-12-19 13:30 -0800 |
| Message-ID | <20586231.126.1324330236379.JavaMail.geo-discussion-forums@yqcb25> |
| In reply to | #4096 |
Thanks for the hand!
[toc] | [prev] | [next] | [standalone]
| From | Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> |
|---|---|
| Date | 2011-12-19 23:47 +0100 |
| Message-ID | <jcoetq$qod$1@news.albasani.net> |
| In reply to | #4096 |
Jerry Stuckle schrieb: > On 12/19/2011 3:36 PM, Mike wrote: >> Thanks... suppose the keys are not in a sortable order, like this: >> >> $toImplode = Array( >> "second" = "Second", >> "fourth" = "Fourth", >> "third" = "Third", >> "first" = "First" >> ); > Use usort() and define your own comparison function. Rather u*k*sort() for sorting by keys: http://de3.php.net/manual/en/function.uksort.php Greetings, Thomas -- Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison! (Coluche)
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2011-12-20 02:59 +0100 |
| Message-ID | <1673804.JFYEL58v44@PointedEars.de> |
| In reply to | #4098 |
Thomas Mlynarczyk wrote:
> Jerry Stuckle schrieb:
>> Mike wrote:
>>> Thanks... suppose the keys are not in a sortable order, like this:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>> $toImplode = Array(
>>> "second" = "Second",
>>> "fourth" = "Fourth",
>>> "third" = "Third",
>>> "first" = "First"
>>> );
>
>> Use usort() and define your own comparison function.
>
> Rather u*k*sort() for sorting by keys:
> http://de3.php.net/manual/en/function.uksort.php
See the marked part.
You should also post the (manual) URI that works best for the person using
it, which would be
<http://php.net/uksort>
in this case.
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
[toc] | [prev] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2011-12-19 22:34 -0500 |
| Message-ID | <jcovo0$an2$1@dont-email.me> |
| In reply to | #4102 |
On 12/19/2011 8:59 PM, Thomas 'PointedEars' Lahn wrote: > Thomas Mlynarczyk wrote: > >> Jerry Stuckle schrieb: >>> Mike wrote: >>>> Thanks... suppose the keys are not in a sortable order, like this: > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >>>> $toImplode = Array( >>>> "second" = "Second", >>>> "fourth" = "Fourth", >>>> "third" = "Third", >>>> "first" = "First" >>>> ); >> >>> Use usort() and define your own comparison function. >> >> Rather u*k*sort() for sorting by keys: >> http://de3.php.net/manual/en/function.uksort.php > > See the marked part. > > You should also post the (manual) URI that works best for the person using > it, which would be > > <http://php.net/uksort> > > in this case. > > > PointedEars Who gives a damn what a troll thinks? -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ==================
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2011-12-20 02:55 +0100 |
| Message-ID | <1820969.7NaK4W3vae@PointedEars.de> |
| In reply to | #4095 |
Mike wrote:
> Thanks... suppose the keys are not in a sortable order, like this:
>
> $toImplode = Array(
> "second" = "Second",
> "fourth" = "Fourth",
> "third" = "Third",
> "first" = "First"
> );
This is not PHP code.
> Or would I need to simply:
>
> echo $toImplode["first"] . $toImplode["second"]; // etc.
No, aside from u(k)sort() you can use a loop to build a new array:
$a = array();
foreach (array('first', 'second', 'third', 'fourth') as $key)
{
$a[] = $toImplode[$key];
}
echo implode(',', $a);
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7@news.demon.co.uk>
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web