FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » implode() order?
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
implode() order? [message #176294] Mon, 19 December 2011 20:13 Go to next message
Mike is currently offline  Mike
Messages: 18
Registered: December 2010
Karma: 0
Junior Member
How can I specify the order that an Array() is imploded? See code below:

<?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"
 */
?>
Re: implode() order? [message #176295 is a reply to message #176294] Mon, 19 December 2011 20:15 Go to previous messageGo to next message
Mike is currently offline  Mike
Messages: 18
Registered: December 2010
Karma: 0
Junior Member
The actual code uses named keys:

$toImplode = Array(
  "b" => "Second",
  "a" => "First",
  "c" => "Third"
);
Re: implode() order? [message #176296 is a reply to message #176294] Mon, 19 December 2011 20:23 Go to previous messageGo to next message
Thomas Mlynarczyk is currently offline  Thomas Mlynarczyk
Messages: 131
Registered: September 2010
Karma: 0
Senior Member
Mike schrieb:
> How can I specify the order that an Array() is imploded? See code below:
>
>
[/color]
[color=blue]>  <?php[/color]
[color=blue]>  $toImplode = Array([/color]
[color=blue]>  	1 => "Second", [/color]
[color=blue]>  	0 => "First", [/color]
[color=blue]>  	2 => "Third"[/color]
[color=blue]>  );[/color]
[color=blue]>  [/color]
[color=blue]>  $finalString = implode(" - ", $toImplode);[/color]
[color=blue]>  [/color]
[color=blue]>  echo "<p>" . $finalString . "</p>";[/color]
[color=blue]>  [/color]
[color=blue]>  /* Results as "Second - First - Third"[/color]
[color=blue]>   * But I need it to be "First - Second - Third"[/color]
[color=blue]>   */[/color]
[color=blue]>  ?>[/color]
[color=blue]>  
[/color]

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)
Re: implode() order? [message #176298 is a reply to message #176296] Mon, 19 December 2011 20:36 Go to previous messageGo to next message
Mike is currently offline  Mike
Messages: 18
Registered: December 2010
Karma: 0
Junior Member
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.
Re: implode() order? [message #176299 is a reply to message #176298] Mon, 19 December 2011 21:15 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
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(at)attglobal(dot)net
==================
Re: implode() order? [message #176300 is a reply to message #176299] Mon, 19 December 2011 21:30 Go to previous messageGo to next message
Mike is currently offline  Mike
Messages: 18
Registered: December 2010
Karma: 0
Junior Member
Thanks for the hand!
Re: implode() order? [message #176301 is a reply to message #176299] Mon, 19 December 2011 22:47 Go to previous messageGo to next message
Thomas Mlynarczyk is currently offline  Thomas Mlynarczyk
Messages: 131
Registered: September 2010
Karma: 0
Senior Member
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)
Re: implode() order? [message #176305 is a reply to message #176301] Tue, 20 December 2011 01:59 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
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.)
Re: implode() order? [message #176306 is a reply to message #176298] Tue, 20 December 2011 01:55 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
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(at)news(dot)demon(dot)co(dot)uk>
Re: implode() order? [message #176307 is a reply to message #176305] Tue, 20 December 2011 03:34 Go to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
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(at)attglobal(dot)net
==================
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: implode() order?
Next Topic: FastCGI + ignore_user_abort
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 30 23:34:39 GMT 2024

Total time taken to generate the page: 0.02457 seconds