Jump to content

array_multisort() issues


kobb

Recommended Posts

Hi all,

I'm actually having problems on how to sort my array.

Here is the skeleton :

[code]Array
(
    [0] => Array
        (
            [file] => CV_A.DOC
            [written] => 2007-01-24 12:00:47
            [size] => 30,0 Ko
            [line] => Array
                (
                    [0] =>  - Prise en charge

                    [1] =>

                    [2] =>
                )

            [link] => ../../stock/cv/CV_A.DOC
        )

    [1] => Array
        (
            [file] => CV_S.doc
            [written] => 2007-01-22 14:29:47
            [size] => 774,5 Ko
            [line] => Array
                (
                    [0] => Sept  2003  Juil  2005

                    [1] =>

                    [2] =>
                )

            [link] => ../../stock/cv/CV_S.doc
        )

    [2] => Array
        (
            [file] => CV_D.doc
            [written] => 2007-01-24 08:51:21
            [size] => 29,0 Ko
            [line] => Array
                (
                    [0] => Transport

                    [1] =>

                    [2] =>
                )

            [link] => ../../stock/cv/CV_D.doc
        )
)[/code]

I would like the function to sort it out and make it look like this :
[code]Array
(
    [0] => Array
        (
            [file] => CV_A.DOC
            [written] => 2007-01-24 12:00:47
            [size] => 30,0 Ko
            [line] => Array
                (
                    [0] =>  - Prise en charge

                    [1] =>

                    [2] =>
                )

            [link] => ../../stock/cv/CV_A.DOC
        )

    [1] => Array
        (
            [file] => CV_S.doc
            [written] => 2007-01-22 14:29:47
            [size] => 774,5 Ko
            [line] => Array
                (
                    [0] => Sept  2003  Juil  2005

                    [1] =>

                    [2] =>
                )

            [link] => ../../stock/cv/CV_S.doc
        )

    [2] => Array
        (
            [file] => CV_D.doc
            [written] => 2007-01-24 08:51:21
            [size] => 29,0 Ko
            [line] => Array
                (
                    [0] => Transport

                    [1] =>

                    [2] =>
                )

            [link] => ../../stock/cv/CV_D.doc
        )
)[/code]

I'm trying to sort it by 'file' but the array can be unlimited by it's first dimension :

$tab[0]['file'] = "yo"
$tab[0]['written'] = "yo"
$tab[0]['size'] = "yo"
$tab[0]['line'][0] = "line 1"
$tab[0]['line'][1] = "line 2"
$tab[0]['link'] = "yo"

$tab[1]['file'] = "yo"
$tab[1]['written'] = "yo"
$tab[1]['size'] = "yo"
$tab[1]['line'][0] = "line 1"
$tab[1]['line'][1] = "line 2"
$tab[1]['link'] = "yo"

...

There can be as many $tab[...] as possible, whereas the second dimension
is fixed to 5.
The ['line'] can also be unlimited.

I just want to order this array and I can't get it done.

Any answers would be appreciated.

Thanks to all !

Cyrille

P.S. Sorry for the bad english !
Link to comment
https://forums.phpfreaks.com/topic/35797-array_multisort-issues/
Share on other sites

Ok, your two example arrays above are EXACTLY the same - no difference in order. However, you do state that you want to order by ['file'] so I will respond to that.

Using array_multisort will require you to convert your arrays. I suggest using usort like this
[code]<?php
function sortByFile($a, $b) {
  if ($a['file'] == $b['file']) { return 0; }
  return ($a['file'] < $b['file']) ? -1 : 1;
}

usort($tab, "sortByFile");
?>[/code]

Archived

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

×
×
  • 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.