Jump to content

Archived

This topic is now archived and is closed to further replies.

Guest t0rtilla

How can i sort an array like this?

Recommended Posts

Guest t0rtilla
[code]
<?php
Array (
      [pnam] => gombi1
      [con] => santachaos
      [crnk] => rfss
      [div] => -
      [time] => 15.07.2006 12:52
      [mod] => 15.07.2006 22:59
      [ip] => 84.50.16.93
      )
Array (
      [pnam] => obstgrf
      [con] => obstgrf
      [crnk] => obstgrf
      [div] => -
      [time] => July 15 2006 23:30:47.
      [mod] => Not traced yet
      [ip] => Not traced yet
      )
?>[/code]

ive tryed array_multy_sort and it dint work for me.

Share this post


Link to post
Share on other sites
Guest t0rtilla
[quote author=king arthur link=topic=104356.msg416205#msg416205 date=1155653267]
Which key do you want to sort on?
[/quote]
[code][crnk][/code]

Share this post


Link to post
Share on other sites
Use ksort if you wan't to sort the array after the keys. (ksort = key sort)

Share this post


Link to post
Share on other sites
Okay, for multi-dimensional arrays you need a usort. Sounds scary but easy really.

First you define a function:

[code]
<?php
function sort_on_crnk($x, $y)
{
if($x['crnk'] == $y['crnk']) return 0;
if($x['crnk'] < $y['crnk']) return -1;
return 1;
}
?>
[/code]

Now you just do

[code]
usort($your_array, 'sort_on_crnk');
[/code]

HTH.

Share this post


Link to post
Share on other sites
Well I'm assuming he meant
[code]
<?php
$your_array = array(
array (
      [pnam] => gombi1
      [con] => santachaos
      [crnk] => rfss
      [div] => -
      [time] => 15.07.2006 12:52
      [mod] => 15.07.2006 22:59
      [ip] => 84.50.16.93
      )
array (
      [pnam] => obstgrf
      [con] => obstgrf
      [crnk] => obstgrf
      [div] => -
      [time] => July 15 2006 23:30:47.
      [mod] => Not traced yet
      [ip] => Not traced yet
      )
)
?>
[/code]

but perhaps he didn't.

Share this post


Link to post
Share on other sites
the array is multidiensional as it has two array elements.

I think lol.........................

looking at king arthur example defintly.

Share this post


Link to post
Share on other sites
Guest t0rtilla
[code]
<?php
$id_pool = array(
                'pnam' => $id[1],
                'con'  => $id[2],
                'crnk' => $id[3],
                'div'  => $id[4],
                'time' => $id[5],
                'mod'  => $id[6],
                'ip'  => $id[7]
                );
?>
[/code]
output will be (example):
[code]
<?php
Array (
      [pnam] => gombi1
      [con] => santachaos
      [crnk] => rfss
      [div] => -
      [time] => 15.07.2006 12:52
      [mod] => 15.07.2006 22:59
      [ip] => 84.50.16.93
      )
Array (
      [pnam] => obstgrf
      [con] => obstgrf
      [crnk] => obstgrf
      [div] => -
      [time] => July 15 2006 23:30:47.
      [mod] => Not traced yet
      [ip] => Not traced yet
      )
?>
[/code]

and i wanted to sort  by crnk

Share this post


Link to post
Share on other sites
[quote author=redarrow link=topic=104356.msg416221#msg416221 date=1155654218]
the array is multidiensional as it has two array elements.

I think lol.........................

looking at king arthur example defintly.
[/quote]

No, this is a multidimensional array:

[code]<?php
$array = array(
array('hi','hi2','hi3'),
'hi again',
);
?>[/code]

Since it have more "dimensions"

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.