Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #4149 > unrolled thread
| Started by | Michael Joel <no@please.com> |
|---|---|
| First post | 2011-12-27 15:25 -0500 |
| Last post | 2011-12-27 22:50 -0500 |
| Articles | 8 — 4 participants |
Back to article view | Back to comp.lang.php
multi-dimensional array sorting by index (like a table) Michael Joel <no@please.com> - 2011-12-27 15:25 -0500
Re: multi-dimensional array sorting by index (like a table) The Natural Philosopher <tnp@invalid.invalid> - 2011-12-27 21:07 +0000
Re: multi-dimensional array sorting by index (like a table) Denis McMahon <denismfmcmahon@gmail.com> - 2011-12-27 23:16 +0000
Re: multi-dimensional array sorting by index (like a table) Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2011-12-28 01:03 +0100
Re: multi-dimensional array sorting by index (like a table) Michael Joel <no@please.com> - 2011-12-27 19:24 -0500
Re: multi-dimensional array sorting by index (like a table) Denis McMahon <denismfmcmahon@gmail.com> - 2011-12-28 00:39 +0000
Re: multi-dimensional array sorting by index (like a table) Michael Joel <no@please.com> - 2011-12-27 20:12 -0500
Re: multi-dimensional array sorting by index (like a table) Michael Joel <no@please.com> - 2011-12-27 22:50 -0500
| From | Michael Joel <no@please.com> |
|---|---|
| Date | 2011-12-27 15:25 -0500 |
| Subject | multi-dimensional array sorting by index (like a table) |
| Message-ID | <ndakf758sqreqh354sjndhrbdii604ddgj@4ax.com> |
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
[toc] | [next] | [standalone]
| From | The Natural Philosopher <tnp@invalid.invalid> |
|---|---|
| Date | 2011-12-27 21:07 +0000 |
| Message-ID | <jddc2h$i8o$3@news.albasani.net> |
| In reply to | #4149 |
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....
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2011-12-27 23:16 +0000 |
| Message-ID | <4efa51d3$0$16709$a8266bb1@newsreader.readnews.com> |
| In reply to | #4149 |
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
[toc] | [prev] | [next] | [standalone]
| From | Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> |
|---|---|
| Date | 2011-12-28 01:03 +0100 |
| Message-ID | <jddmcj$7bu$1@news.albasani.net> |
| In reply to | #4149 |
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)
[toc] | [prev] | [next] | [standalone]
| From | Michael Joel <no@please.com> |
|---|---|
| Date | 2011-12-27 19:24 -0500 |
| Message-ID | <paokf7tiaqvjhf89df6hp4nh6qr9qt9km7@4ax.com> |
| In reply to | #4156 |
On Wed, 28 Dec 2011 01:03:22 +0100, Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.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
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2011-12-28 00:39 +0000 |
| Message-ID | <4efa6547$0$16669$a8266bb1@newsreader.readnews.com> |
| In reply to | #4157 |
On Tue, 27 Dec 2011 19:24:05 -0500, Michael Joel wrote:
> On Wed, 28 Dec 2011 01:03:22 +0100, Thomas Mlynarczyk
> <thomas@mlynarczyk-webdesign.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
[toc] | [prev] | [next] | [standalone]
| From | Michael Joel <no@please.com> |
|---|---|
| Date | 2011-12-27 20:12 -0500 |
| Message-ID | <h3rkf7hpvr7u3189jfl7mmp8rrm0qulafj@4ax.com> |
| In reply to | #4158 |
On 28 Dec 2011 00:39:35 GMT, Denis McMahon <denismfmcmahon@gmail.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@mlynarczyk-webdesign.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
[toc] | [prev] | [next] | [standalone]
| From | Michael Joel <no@please.com> |
|---|---|
| Date | 2011-12-27 22:50 -0500 |
| Message-ID | <mb4lf7ph88sk05tkm1u7n3ebthontt7rvp@4ax.com> |
| In reply to | #4159 |
On Tue, 27 Dec 2011 20:12:04 -0500, Michael Joel <no@please.com> wrote: >On 28 Dec 2011 00:39:35 GMT, Denis McMahon <denismfmcmahon@gmail.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@mlynarczyk-webdesign.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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web