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

Home » Imported messages » comp.lang.php » multi-dimensional array sorting by index (like a table)
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
multi-dimensional array sorting by index (like a table) [message #176350] Tue, 27 December 2011 20:25 Go to next message
Michael Joel is currently offline  Michael Joel
Messages: 42
Registered: October 2011
Karma: 0
Member
I am guessing there is no php function to handle sorting an array as
you could a database? Meaning I want a multi-dimensional array sorted
by index 0, and "sub sorting" (whatever you call it) using index 4?

Thanks
Mike
Re: multi-dimensional array sorting by index (like a table) [message #176355 is a reply to message #176350] Tue, 27 December 2011 21:07 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Michael Joel wrote:
> I am guessing there is no php function to handle sorting an array as
> you could a database? Meaning I want a multi-dimensional array sorted
> by index 0, and "sub sorting" (whatever you call it) using index 4?
>
> Thanks
> Mike
write one.
:-)

Then there is....
Re: multi-dimensional array sorting by index (like a table) [message #176356 is a reply to message #176350] Tue, 27 December 2011 23:16 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Tue, 27 Dec 2011 15:25:29 -0500, Michael Joel wrote:

> I am guessing there is no php function to handle sorting an array as you
> could a database? Meaning I want a multi-dimensional array sorted by
> index 0, and "sub sorting" (whatever you call it) using index 4?

Yes, you write a user sort that compares two elements, something like:

function mysort($a, $b) {
if ($a[0] > $b[0]) return 1;
if ($a[0] < $b[0]) return -1;
if ($a[4] > $b[4]) return 1;
if ($a[4] < $a[4]) return -1;
return 0;
}

then

usort($arr, "mysort");

You might want error handling in mysort to make sure that $a and $b are
both actually arrays of at least 5 elements size.

Rgds

Denis McMahon
Re: multi-dimensional array sorting by index (like a table) [message #176357 is a reply to message #176350] Wed, 28 December 2011 00:03 Go to previous messageGo to next message
Thomas Mlynarczyk is currently offline  Thomas Mlynarczyk
Messages: 131
Registered: September 2010
Karma: 0
Senior Member
Michael Joel schrieb:
> I am guessing there is no php function to handle sorting an array as
> you could a database? Meaning I want a multi-dimensional array sorted
> by index 0, and "sub sorting" (whatever you call it) using index 4?

Have a look at http://de3.php.net/array_multisort.

Greetings,
Thomas

--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
Re: multi-dimensional array sorting by index (like a table) [message #176358 is a reply to message #176357] Wed, 28 December 2011 00:24 Go to previous messageGo to next message
Michael Joel is currently offline  Michael Joel
Messages: 42
Registered: October 2011
Karma: 0
Member
On Wed, 28 Dec 2011 01:03:22 +0100, Thomas Mlynarczyk
<thomas(at)mlynarczyk-webdesign(dot)de> wrote:

> Michael Joel schrieb:
>> I am guessing there is no php function to handle sorting an array as
>> you could a database? Meaning I want a multi-dimensional array sorted
>> by index 0, and "sub sorting" (whatever you call it) using index 4?
>
> Have a look at http://de3.php.net/array_multisort.
>
> Greetings,
> Thomas


Yes I saw that but I couldn't seem to get it to work.
It says it will sort a multi-dimensional array by an index.
I tried and it just kept spitting errors at me. I test the arrays to
make sure they contained data I expected.

Mike
Re: multi-dimensional array sorting by index (like a table) [message #176359 is a reply to message #176358] Wed, 28 December 2011 00:39 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Tue, 27 Dec 2011 19:24:05 -0500, Michael Joel wrote:

> On Wed, 28 Dec 2011 01:03:22 +0100, Thomas Mlynarczyk
> <thomas(at)mlynarczyk-webdesign(dot)de> wrote:

>> Michael Joel schrieb:

>>> I am guessing there is no php function to handle sorting an array as
>>> you could a database? Meaning I want a multi-dimensional array sorted
>>> by index 0, and "sub sorting" (whatever you call it) using index 4?

>> Have a look at http://de3.php.net/array_multisort.

> Yes I saw that but I couldn't seem to get it to work. It says it will
> sort a multi-dimensional array by an index. I tried and it just kept
> spitting errors at me. I test the arrays to make sure they contained
> data I expected.

The way I read it (example 3), assuming your 2d array is $data containing
1d arrays of 5 or more elements:

$k1 = $k2 = array();
foreach ($data as $key => $row) {
$k1[$key] = $row[0];
$k2[$key] = $row[4];
}
array_multisort($k1, SORT_DESC, $k2, SORT_DESC, $data);

but I haven't tried this.

Mind you, I haven't tried the other solution that I posted earlier
either, although I have used similar code in practice.

Rgds

Denis McMahon
Re: multi-dimensional array sorting by index (like a table) [message #176360 is a reply to message #176359] Wed, 28 December 2011 01:12 Go to previous messageGo to next message
Michael Joel is currently offline  Michael Joel
Messages: 42
Registered: October 2011
Karma: 0
Member
On 28 Dec 2011 00:39:35 GMT, Denis McMahon <denismfmcmahon(at)gmail(dot)com>
wrote:

> On Tue, 27 Dec 2011 19:24:05 -0500, Michael Joel wrote:
>
>> On Wed, 28 Dec 2011 01:03:22 +0100, Thomas Mlynarczyk
>> <thomas(at)mlynarczyk-webdesign(dot)de> wrote:
>
>>> Michael Joel schrieb:
>
>>>> I am guessing there is no php function to handle sorting an array as
>>>> you could a database? Meaning I want a multi-dimensional array sorted
>>>> by index 0, and "sub sorting" (whatever you call it) using index 4?
>
>>> Have a look at http://de3.php.net/array_multisort.
>
>> Yes I saw that but I couldn't seem to get it to work. It says it will
>> sort a multi-dimensional array by an index. I tried and it just kept
>> spitting errors at me. I test the arrays to make sure they contained
>> data I expected.
>
> The way I read it (example 3), assuming your 2d array is $data containing
> 1d arrays of 5 or more elements:
>
> $k1 = $k2 = array();
> foreach ($data as $key => $row) {
> $k1[$key] = $row[0];
> $k2[$key] = $row[4];
> }
> array_multisort($k1, SORT_DESC, $k2, SORT_DESC, $data);
>
> but I haven't tried this.
>
> Mind you, I haven't tried the other solution that I posted earlier
> either, although I have used similar code in practice.
>
> Rgds
>
> Denis McMahon


Ahh. I see how it is working now.
This I think is what I am needing - will test it.

Thanks all
Mike
Re: multi-dimensional array sorting by index (like a table) [message #176361 is a reply to message #176360] Wed, 28 December 2011 03:50 Go to previous message
Michael Joel is currently offline  Michael Joel
Messages: 42
Registered: October 2011
Karma: 0
Member
On Tue, 27 Dec 2011 20:12:04 -0500, Michael Joel <no(at)please(dot)com>
wrote:

> On 28 Dec 2011 00:39:35 GMT, Denis McMahon <denismfmcmahon(at)gmail(dot)com>
> wrote:
>
>> On Tue, 27 Dec 2011 19:24:05 -0500, Michael Joel wrote:
>>
>>> On Wed, 28 Dec 2011 01:03:22 +0100, Thomas Mlynarczyk
>>> <thomas(at)mlynarczyk-webdesign(dot)de> wrote:
>>
>>>> Michael Joel schrieb:
>>
> ......................
> Ahh. I see how it is working now.
> This I think is what I am needing - will test it.
>
> Thanks all
> Mike

Yes it is working it appears.
That saves a lot of work.
I can use one table instead of 2 or 3.

Thanks all
Mike
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: standard easy way to set up a registration/logon cookie?
Next Topic: is the php.net server down?
Goto Forum:
  

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

Current Time: Fri Nov 22 02:31:31 GMT 2024

Total time taken to generate the page: 0.02532 seconds