sorting array...i

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

    #1

    sorting array...i

    Hi,
    I am getting confused with sorting arrays...
    $arraytest =array(5) { [0]=string(2) "39" [1]=string(2) "44" [2]=>
    string(2) "77" [3]=string(3) "150" [4]=string(3) "464" }

    why do i get NULL value if i try to sort this array using sort command?

    sort($arraytest );


    Thanks for your help!
    VooDoo


  • Steve

    #2
    Re: sorting array...i


    "VooDoo" <VooDooNet38@fr ee.frwrote in message
    news:fhuulb$72s $1@reader1.imag inet.fr...
    Hi,
    I am getting confused with sorting arrays...
    $arraytest =array(5) { [0]=string(2) "39" [1]=string(2) "44" [2]=>
    string(2) "77" [3]=string(3) "150" [4]=string(3) "464" }
    >
    why do i get NULL value if i try to sort this array using sort command?
    >
    sort($arraytest );
    poorly asked question.

    sort() performs a sort on the $array parameter by reference. sort() does NOT
    return the sorted version of $array as a result of its operation. so...

    <?
    $array = array(
    '39' ,
    '44' ,
    '77' ,
    '150' ,
    '464'
    );
    sort($array);
    echo '<pre>' . print_r($array, true) . '</pre>';
    ?>

    will print the array and show that the values are sorted from lowest to
    highest.

    btw, leave out the array(5) string(2) [0]=shit. it makes reading the code
    a pain in the ass and isn't needed in the least...it doesn't make any impact
    on the way php executes that code. if coded like the above, you can easily
    see what *actual* values are making up the members of the array...rather
    than trying to weed out the other junk surrounding them (like what index it
    has or what data-type you are *casting* it to). the extra crap is feckless
    and less than useful.


    Comment

    • Rik Wasmus

      #3
      Re: sorting array...i

      On Tue, 20 Nov 2007 16:32:13 +0100, VooDoo <VooDooNet38@fr ee.frwrote:
      Hi,
      I am getting confused with sorting arrays...
      $arraytest =array(5) { [0]=string(2) "39" [1]=string(2) "44"[2]=>
      string(2) "77" [3]=string(3) "150" [4]=string(3) "464" }
      >
      why do i get NULL value if i try to sort this array using sort command?
      >
      sort($arraytest );
      Probably, because sort() works by reference, not return:

      $arraytest = sort($arraytest );
      //=>$arraytest = true or false; $arraytest[N] = NULL

      sort($arraytest );
      //=>$arraytest = sorted array; $arraytest[N] = set value or NULL if
      non-existant
      --
      Rik Wasmus

      Comment

      • VooDoo

        #4
        Re: sorting array...i

        Ok thanks for your replies.
        So the sort its working, I do not need to affect it to a var, just call the
        function...

        Sorry for the extra crap it was the outpout of a var_dump as i could not
        understand what was wrong.
        Thanks
        Voodoo


        Comment

        • Steve

          #5
          Re: sorting array...i


          "VooDoo" <VooDooNet38@fr ee.frwrote in message
          news:fi0ui3$e98 $1@reader1.imag inet.fr...
          Ok thanks for your replies.
          So the sort its working, I do not need to affect it to a var, just call
          the function...
          right.
          Sorry for the extra crap it was the outpout of a var_dump as i could not
          understand what was wrong.
          Thanks
          no problem.


          Comment

          Working...