Jump to content


How can i sort an array like this?


  • Please log in to reply
10 replies to this topic

#1 Guest_t0rtilla_*

Guest_t0rtilla_*
  • Guests

Posted 15 August 2006 - 02:42 PM

<?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
      )
?>

ive tryed array_multy_sort and it dint work for me.

#2 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 15 August 2006 - 02:47 PM

Which key do you want to sort on?
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#3 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 15 August 2006 - 02:50 PM

natsort, sort, ksort. There are plenty to chose from.

#4 Guest_t0rtilla_*

Guest_t0rtilla_*
  • Guests

Posted 15 August 2006 - 02:54 PM

Which key do you want to sort on?

[crnk]


#5 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 15 August 2006 - 02:55 PM

Use ksort if you wan't to sort the array after the keys. (ksort = key sort)

#6 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 15 August 2006 - 02:58 PM

Okay, for multi-dimensional arrays you need a usort. Sounds scary but easy really.

First you define a function:

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

Now you just do

usort($your_array, 'sort_on_crnk');

HTH.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#7 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 15 August 2006 - 03:01 PM

His array is not multi-dimensional.

#8 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 15 August 2006 - 03:03 PM

Well I'm assuming he meant
<?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
      )
)
?>

but perhaps he didn't.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#9 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 15 August 2006 - 03:03 PM

the array is multidiensional as it has two array elements.

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

looking at king arthur example defintly.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#10 Guest_t0rtilla_*

Guest_t0rtilla_*
  • Guests

Posted 15 August 2006 - 03:12 PM

<?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]
                 );
?>
output will be (example):
<?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
      )
?>

and i wanted to sort  by crnk

#11 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 15 August 2006 - 03:17 PM

the array is multidiensional as it has two array elements.

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

looking at king arthur example defintly.


No, this is a multidimensional array:

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

Since it have more "dimensions"




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users