Jump to content

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.
Link to comment
https://forums.phpfreaks.com/topic/17631-how-can-i-sort-an-array-like-this/
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.
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.
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
[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"
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.