Sorting a Multidimensional Array

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default Sorting a Multidimensional Array

    I have an array like this:

    $events = array(
    array(
    '2003-07-01',
    'Event Title 1',
    '1' //ID Number (not unique)
    ),
    array(
    '2003-07-02',
    'Event Title 3',
    '22'
    ),
    array(
    '2003-07-06',
    'Event Title 3',
    '35'
    )
    );


    I want to sort it by date. How would I do it?

    I read the documentation for usort(), but I can't get it to work right.
    Any suggestions?
    Brian Guest

  2. Similar Questions and Discussions

    1. creating a multidimensional array
      How can I combine two arrays in cfscript to form one multidimensional array? For example, I have two arrays: firstarray=fred; firstarray=jim;...
    2. sorting multidimensional arrays
      One last question about multi-dimensional arrays - I'm aware of the function array_multisort, but that keeps the relationship with the key - what do...
    3. Problem with multidimensional array
      Hi! I'm new on this forum, and in the Macromedia Flash development. I've read some books, like the Flash MX 2004 game development (C. S. Murray,...
    4. Multidimensional array: see if 1st key is available
      Hi, I've got an multidimensional array $ret = $country_code; Now I want to see if $countryCode is even in that array, because if it's not, it...
    5. Split multidimensional array into 4 multidimensional arrays
      Hello everyone, I have a multidimensional array that I need to split into 4 multidimensional arrays. I've tried the examples from the...
  3. #2

    Default Re: Sorting a Multidimensional Array

    Thank you so much, Jason. I didn't understand what the extra function
    was for until you posted that.

    It works perfectly, now. Thanks again.


    >
    > Sorting by date from the usort docs on php.net - sorting a
    > multidimensional array:
    >
    > <begin>
    > function cmp ($a, $b) {
    > //return strcmp($a["fruit"], $b["fruit"]);
    > // in the case of the above array
    > // note we are comparing the first element of each array
    > if ( $a[0] < $b[0] ) {
    > return -1;
    > }
    > if ( $a[0] > $b[0] ) {
    > return 1;
    > }
    > // they are equal
    > return 0;
    > }
    >
    > // we're using the array supplied above by Brian
    > /*
    > $fruits[0]["fruit"] = "lemons";
    > $fruits[1]["fruit"] = "apples";
    > $fruits[2]["fruit"] = "grapes";
    > */
    >
    > // again, using data supplied by Brian
    > usort($events, "cmp");
    > //usort($fruits, "cmp");
    > <end>
    >
    > This should sort the multidimensional array by the first element (index
    > 0). Incidentally, you could make it sort in descending order by
    > switching the 1 and -1 in the "cmp" function.
    >
    > Also, doing this in objects gets kind of tricky, read the user supplied
    > notes, there's a lot of useful stuff in there.
    >
    > Jason
    >
    >
    Brian Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139