array_multisort() not working

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • michael nieuwenhuizen

    array_multisort() not working

    <newbie alert>

    i have 2 related arrays that i wanna sort at once. if i try:

    print_r($my_arr ay1);
    array_multisort ($my_array1, SORT_ASC, $my_array2, SORT_ASC);
    print_r($my_arr ay1);

    both arrays remain unsorted. if i try:

    print_r($my_arr ay1);
    sort($my_array1 );
    print_r($my_arr ay1);

    $my_array1 gets sorted. what am i doing wrong? both arrays
    are indexed array and they have an equal amount of entries.

    mike


  • Andy Hassall

    #2
    Re: array_multisort () not working

    On Tue, 13 Jan 2004 17:06:47 +0100, "michael nieuwenhuizen" <usenet@mikkie. net>
    wrote:
    [color=blue]
    ><newbie alert>
    >
    >i have 2 related arrays that i wanna sort at once. if i try:
    >
    > print_r($my_arr ay1);
    > array_multisort ($my_array1, SORT_ASC, $my_array2, SORT_ASC);
    > print_r($my_arr ay1);
    >
    >both arrays remain unsorted. if i try:
    >
    > print_r($my_arr ay1);
    > sort($my_array1 );
    > print_r($my_arr ay1);
    >
    >$my_array1 gets sorted. what am i doing wrong? both arrays
    >are indexed array and they have an equal amount of entries.[/color]

    PHP version?
    Can you post example data (i.e. something runnable) and output?

    --
    Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
    <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>

    Comment

    • michael nieuwenhuizen

      #3
      Re: array_multisort () not working


      "Andy Hassall" <andy@andyh.co. uk> wrote[color=blue][color=green]
      >>
      >> i have 2 related arrays that i wanna sort at once. if i try:
      >>
      >> print_r($my_arr ay1);
      >> array_multisort ($my_array1, SORT_ASC, $my_array2, SORT_ASC);
      >> print_r($my_arr ay1);
      >>
      >> both arrays remain unsorted. if i try:
      >>
      >> print_r($my_arr ay1);
      >> sort($my_array1 );
      >> print_r($my_arr ay1);
      >>
      >> $my_array1 gets sorted. what am i doing wrong? both arrays
      >> are indexed array and they have an equal amount of entries.[/color]
      > PHP version?[/color]

      4.3.2.
      [color=blue]
      > Can you post example data (i.e. something runnable)[/color]

      not sure if it's a good idea to post this all, but here we go.

      ==== code =============

      <?php

      $file = 'dvds.xml';

      $current_tag = "";
      $current_item = -1;

      // init arrays
      $year = array(); // year
      $var1 = array(); // dvd: name; cd: artist; travel: country

      // start element
      function startElement($p arser, $tagname, $attrs) {
      global $current_item, $current_tag;
      $current_tag = $tagname;
      switch ($tagname) {
      case "ITEM":
      $current_item++ ;
      $year[$current_item] = 0;
      $var1[$current_item] = "";
      break;
      }
      }

      // end element
      function endElement($par ser, $tagname) {
      }

      // character data
      function characterData($ parser, $data) {
      global $current_item, $current_tag, $year, $var1;
      switch ($current_tag) {
      case "YEAR":
      if ($year[$current_item] == 0) {
      $year[$current_item] = $data;
      }
      break;
      case "VAR1":
      if ($var1[$current_item] == "") {
      $var1[$current_item] = $data;
      }
      break;
      }
      }

      // set parser variables
      $xml_parser = xml_parser_crea te();
      xml_parser_set_ option($xml_par ser, XML_OPTION_CASE _FOLDING, true);
      xml_set_element _handler($xml_p arser, "startEleme nt", "endElement ");
      xml_set_charact er_data_handler ($xml_parser, "characterData" );

      // file not found
      if (!($fp = fopen($file, "r"))) {
      die("could not open XML input");
      }

      // xml error
      while ($data = fread($fp, 4096)) {
      if (!xml_parse($xm l_parser, $data, feof($fp))) {
      die(sprintf("XM L error: %s at line %d",
      xml_error_strin g(xml_get_error _code($xml_pars er)),
      xml_get_current _line_number($x ml_parser)));
      }
      }

      // free parser
      xml_parser_free ($xml_parser);

      sort_dvds();
      print_dvds();

      // sort dvds
      function sort_dvds() {
      global $sort, $year, $var1;
      // sort by year
      array_multisort ($year, SORT_ASC, $var1, SORT_ASC);
      }

      // print dvds
      function print_dvds() {
      global $current_item, $sort, $year, $var1;
      // print by year
      echo " <tr>\n <td class=\"pagetit le\">dvd's</td>\n </tr>\n";
      $temp_year = 0;
      for ($i = 1; $i <= $current_item; $i++) {
      if ($year[$i] != $temp_year) {
      echo " <tr>\n <td class=\"title\" >$year[$i]</td>\n
      </tr>\n";
      $temp_year = $year[$i];
      }
      echo " <tr>\n <td class=\"text\"> $var1[$i]</td>\n </tr>\n";
      }
      }

      ?>

      ==== code =============

      it reads an xml file (http://mikkie.net/php/sort/dvds.xml), stores the
      titles and years in two arrays ($var1 and $year) and tries to output
      the list of movies by year.

      maybe the script looks a bit funny, but a) it's part of a larger script
      (sorting in different ways, sorting & outputting different xml files) and
      b) i didn't know ANYTHING of php til monday last week.
      [color=blue]
      > and output?[/color]



      mike


      Comment

      Working...