array multisort question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • annmartinson@gmail.com

    array multisort question

    yes, I've read and tried umpteen of the sort functions and still can't
    get it right. which one do I need???

    /////////////////////////////////////////////
    here is my array:

    $final[0]['name'] = "fire";
    $final[0]['value'] = 0;
    $final[0]['id'] = 1;
    $final[0]['name'] = "air";
    $final[0]['value'] = 0;
    $final[0]['id'] = 2;
    $final[0]['name'] = "infinity";
    $final[0]['value'] = 0;
    $final[0]['id'] = 3;
    $final[0]['name'] = "water";
    $final[0]['value'] = 0;
    $final[0]['id'] = 4;
    $final[0]['name'] = "earth";
    $final[0]['value'] = 0;
    $final[0]['id'] = 5;

    Within my functional code, the 'value' keys are changed dynamically
    based on returns from a form on a website. At this point, I want to
    rearrange the array SORT_NUMERICAL, SORT_DESC so that the first two
    records are the highest value. I then assign these two 'id' keys to
    separate variables and use them to post the info requested back to the
    site visitor.

    I've tried
    --- (1)

    $final2 = arsort($final[value], SORT_NUMERIC);

    $id = $final2[0]['id'];
    $id2 = $final2[1]['id'];

    ---(2)
    $final = array_multisort ($final[value], SORT_NUMERIC, SORT_DESC);

    $id = $final[0]['id'];
    $id2 = $final[1]['id'];

    ---(3)
    $final2 = array_multisort ($final[value], SORT_NUMERIC, SORT_DESC,
    $final[key]);

    $id = $final2[0]['id'];
    $id2 = $final2[1]['id'];


    every one of these gave me various errors.

    help is much appreciated. I'm sure the answer is MUCH simpler than I'm
    managing :)

  • annmartinson@gmail.com

    #2
    Re: array multisort question

    SORRY, here is the correct array ... that's not the problem.

    $final[0]['name'] = "fire";
    $final[0]['value'] = 0;
    $final[0]['id'] = 1;
    $final[1]['name'] = "air";
    $final[1]['value'] = 0;
    $final[1]['id'] = 2;
    $final[2]['name'] = "infinity";
    $final[2]['value'] = 0;
    $final[2]['id'] = 3;
    $final[3]['name'] = "water";
    $final[3]['value'] = 0;
    $final[3]['id'] = 4;
    $final[4]['name'] = "earth";
    $final[4]['value'] = 0;
    $final[4]['id'] = 5;

    ----------------------------------------------------------------
    annmartinson@gm ail.com wrote:
    yes, I've read and tried umpteen of the sort functions and still can't
    get it right. which one do I need???
    >
    /////////////////////////////////////////////
    here is my array:
    >
    $final[0]['name'] = "fire";
    $final[0]['value'] = 0;
    $final[0]['id'] = 1;
    $final[0]['name'] = "air";
    $final[0]['value'] = 0;
    $final[0]['id'] = 2;
    $final[0]['name'] = "infinity";
    $final[0]['value'] = 0;
    $final[0]['id'] = 3;
    $final[0]['name'] = "water";
    $final[0]['value'] = 0;
    $final[0]['id'] = 4;
    $final[0]['name'] = "earth";
    $final[0]['value'] = 0;
    $final[0]['id'] = 5;
    >
    Within my functional code, the 'value' keys are changed dynamically
    based on returns from a form on a website. At this point, I want to
    rearrange the array SORT_NUMERICAL, SORT_DESC so that the first two
    records are the highest value. I then assign these two 'id' keys to
    separate variables and use them to post the info requested back to the
    site visitor.
    >
    I've tried
    --- (1)
    >
    $final2 = arsort($final[value], SORT_NUMERIC);
    >
    $id = $final2[0]['id'];
    $id2 = $final2[1]['id'];
    >
    ---(2)
    $final = array_multisort ($final[value], SORT_NUMERIC, SORT_DESC);
    >
    $id = $final[0]['id'];
    $id2 = $final[1]['id'];
    >
    ---(3)
    $final2 = array_multisort ($final[value], SORT_NUMERIC, SORT_DESC,
    $final[key]);
    >
    $id = $final2[0]['id'];
    $id2 = $final2[1]['id'];
    >
    >
    every one of these gave me various errors.
    >
    help is much appreciated. I'm sure the answer is MUCH simpler than I'm
    managing :)

    Comment

    • Rik

      #3
      Re: array multisort question

      annmartinson@gm ail.com wrote:
      annmartinson@gm ail.com wrote:
      >yes, I've read and tried umpteen of the sort functions and still
      >can't get it right. which one do I need???
      >>
      >/////////////////////////////////////////////
      >here is my array:
      >>
      >$final[0]['name'] = "fire";
      >$final[0]['value'] = 0;
      >$final[0]['id'] = 1;
      >$final[0]['name'] = "air";
      >$final[0]['value'] = 0;
      >$final[0]['id'] = 2;
      >>
      >Within my functional code, the 'value' keys are changed dynamically
      >based on returns from a form on a website. At this point, I want to
      >rearrange the array SORT_NUMERICAL, SORT_DESC so that the first two
      >records are the highest value. I then assign these two 'id' keys to
      >separate variables and use them to post the info requested back to
      >the site visitor.
      >>
      >I've tried
      >--- (1)
      >>
      >$final2 = arsort($final[value], SORT_NUMERIC);
      >>
      >$id = $final2[0]['id'];
      >$id2 = $final2[1]['id'];
      >>
      >---(2)
      >$final = array_multisort ($final[value], SORT_NUMERIC, SORT_DESC);
      >>
      >$id = $final[0]['id'];
      >$id2 = $final[1]['id'];
      >>
      >---(3)
      >$final2 = array_multisort ($final[value], SORT_NUMERIC, SORT_DESC,
      >$final[key]);
      >>
      >$id = $final2[0]['id'];
      >$id2 = $final2[1]['id'];
      >>
      >>
      >every one of these gave me various errors.
      >>
      >help is much appreciated. I'm sure the answer is MUCH simpler than
      >I'm managing :)
      <?php

      $final[0]['name'] = "fire";
      $final[0]['value'] = 0;
      $final[0]['id'] = 1;
      $final[1]['name'] = "air";
      $final[1]['value'] = 2;
      $final[1]['id'] = 2;
      $final[2]['name'] = "infinity";
      $final[2]['value'] = 6;
      $final[2]['id'] = 3;
      $final[3]['name'] = "water";
      $final[3]['value'] = 1;
      $final[3]['id'] = 4;
      $final[4]['name'] = "earth";
      $final[4]['value'] = 10;
      $final[4]['id'] = 5;

      function cmp($a, $b){
      if($a['value']==$b['value']) return 0;
      return ($a['value']>$b['value']) ? -1 : 1;
      }
      usort($final,'c mp');
      print_r($final) ;
      ?>
      --
      Grtz,

      Rik Wasmus


      Comment

      • annmartinson@gmail.com

        #4
        Re: array multisort question

        No idea why this works, but I do appreciate it very much. plugged it
        in, works like a charm. thanks!!

        Comment

        • Rik

          #5
          Re: array multisort question

          annmartinson@gm ail.com wrote:
          No idea why this works, but I do appreciate it very much. plugged it
          in, works like a charm. thanks!!
          Well for future reference, you may need another sort, so a short
          explanation:
          usort($array,'f unction_name');

          function_name is the custom name of a function to sort by. This function
          will get 2 variables to compare, $a and $b, which are 2 values from your
          array, so in this case:

          $a = array('id'=>1,' name'=>'fire,'v alue'=>0);
          Likewise for b.

          This function will have to return either:
          0 when sorting should return equals.
          -1 when $a should go before $b
          1 when $a should come after $b

          Now we only want to sort on the 'value' key, so we in the function we only
          compare the 'value' key of 'a' and 'b'. In normal sorting, the whole array
          $a and $b are compared.

          Grtz,
          --
          Rik Wasmus


          Comment

          Working...