Array sorting differs depending on PHP version

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alvaro G Vicario

    #1

    Array sorting differs depending on PHP version

    I have this test code:


    <html><pre><?

    echo "Versión: ".phpversion(). "\n\n";

    $define_list = array(
    'PRODUCT_LIST_M ODEL' => 0,
    'PRODUCT_LIST_N AME' => 0,
    'PRODUCT_LIST_M ANUFACTURER' => 1,
    'PRODUCT_LIST_P RICE' => 0,
    'PRODUCT_LIST_Q UANTITY' => 1,
    'PRODUCT_LIST_W EIGHT' => 1,
    'PRODUCT_LIST_I MAGE' => 1,
    'PRODUCT_LIST_B UY_NOW' => 1
    );
    arsort($define_ list);
    echo "*** arsort(): ";
    print_r($define _list);
    echo "\n";

    $define_list = array(
    'PRODUCT_LIST_M ODEL' => 0,
    'PRODUCT_LIST_N AME' => 0,
    'PRODUCT_LIST_M ANUFACTURER' => 1,
    'PRODUCT_LIST_P RICE' => 0,
    'PRODUCT_LIST_Q UANTITY' => 1,
    'PRODUCT_LIST_W EIGHT' => 1,
    'PRODUCT_LIST_I MAGE' => 1,
    'PRODUCT_LIST_B UY_NOW' => 1
    );
    asort($define_l ist);
    echo "*** asort(): ";
    print_r($define _list);
    echo "\n";
    ?>


    In two different servers I obtain different results:


    Versión: 4.1.2

    *** arsort(): Array
    (
    [PRODUCT_LIST_MA NUFACTURER] => 1
    [PRODUCT_LIST_QU ANTITY] => 1
    [PRODUCT_LIST_WE IGHT] => 1
    [PRODUCT_LIST_IM AGE] => 1
    [PRODUCT_LIST_BU Y_NOW] => 1
    [PRODUCT_LIST_MO DEL] => 0
    [PRODUCT_LIST_NA ME] => 0
    [PRODUCT_LIST_PR ICE] => 0
    )

    *** asort(): Array
    (
    [PRODUCT_LIST_MO DEL] => 0
    [PRODUCT_LIST_NA ME] => 0
    [PRODUCT_LIST_PR ICE] => 0
    [PRODUCT_LIST_MA NUFACTURER] => 1
    [PRODUCT_LIST_QU ANTITY] => 1
    [PRODUCT_LIST_WE IGHT] => 1
    [PRODUCT_LIST_IM AGE] => 1
    [PRODUCT_LIST_BU Y_NOW] => 1
    )



    Versión: 4.2.2

    *** arsort(): Array
    (
    [PRODUCT_LIST_QU ANTITY] => 1
    [PRODUCT_LIST_IM AGE] => 1
    [PRODUCT_LIST_MA NUFACTURER] => 1
    [PRODUCT_LIST_BU Y_NOW] => 1
    [PRODUCT_LIST_WE IGHT] => 1
    [PRODUCT_LIST_PR ICE] => 0
    [PRODUCT_LIST_NA ME] => 0
    [PRODUCT_LIST_MO DEL] => 0
    )

    *** asort(): Array
    (
    [PRODUCT_LIST_MO DEL] => 0
    [PRODUCT_LIST_PR ICE] => 0
    [PRODUCT_LIST_NA ME] => 0
    [PRODUCT_LIST_BU Y_NOW] => 1
    [PRODUCT_LIST_IM AGE] => 1
    [PRODUCT_LIST_QU ANTITY] => 1
    [PRODUCT_LIST_MA NUFACTURER] => 1
    [PRODUCT_LIST_WE IGHT] => 1
    )



    According to some user contributed notes (manual itself says nothing about
    this) I can't expect to maintain the relative order of keys. Yet I can't
    find the logic of the algorithm. Why do I get different key sortings in
    versions 4.1.2 and 4.2.2?


    --
    --
    -- Álvaro G. Vicario - Burgos, Spain
    --
  • Robert Downes

    #2
    Re: Array sorting differs depending on PHP version

    The key order is not maintained between versions because the code is
    optimised between versions, and this improvement (though it doesn't
    sound like you'd choose that word) has returned the keys in a different
    arbitrary order. But the fact is, the key order seems to always have
    been arbitrary, so if key order matters, asort (and arsort) are not the
    functions for you.

    ksort() sorts by key while maintaining key=>value relations, so that may
    be of interest to you. However, it sounds more like you just want to
    have a consistent order of keys while still being left with a sorted set
    of values. For this I believe you would have to use usort() and define
    your own sorting function. That way you could decide how to sort things
    once and for all. E.g. use asort to group the set into a value-sorted
    set, then go through the value of each one, and each time the value
    changes, sort the preceeding values using ksort() to sort same-vaule
    entries by key. That may be costly, though, and I may not be seeing an
    easier route.
    --
    Bob
    London, UK
    echo Mail fefsensmrrjyahe eoceoq\! | tr "jefroq\!" "@obe.uk"

    Comment

    • Alvaro G Vicario

      #3
      Re: Array sorting differs depending on PHP version

      *** Robert Downes wrote/escribió (Thu, 11 Dec 2003 21:56:04 +0000):[color=blue]
      > The key order is not maintained between versions because the code is
      > optimised between versions, and this improvement (though it doesn't
      > sound like you'd choose that word) has returned the keys in a different
      > arbitrary order. But the fact is, the key order seems to always have
      > been arbitrary, so if key order matters, asort (and arsort) are not the
      > functions for you.[/color]

      Thank you for your answer. I guess that's the logic of the algorithm: it
      isn't the same algorithm :)

      There doesn't seem to be a pre-written function that sorts values keeping
      the relative order of keys but user notes provide with some ideas. Anyway,
      I've checked the code again (it's a code I didn't write) and realized that
      I don't probably need any kind of sorting once I decide an order for the
      fields.

      --
      --
      -- Álvaro G. Vicario - Burgos, Spain
      --

      Comment

      Working...