The different behavior of the function uasort in PHP 5.5 and PHP 7.0

后端 未结 2 1565
迷失自我
迷失自我 2021-01-17 12:16

I encountered a strange behavior of Magento 1.8 after changing php version from 5.5 to 7.0. This strange behavior is due to a change in the work function uasort

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-17 12:48

    From the usort() documentation:

    Note: If two members compare as equal, their relative order in the sorted array is undefined.

    This is what you're seeing here. PHP 7 uses a different, partially stable sorting algorithm, so elements that compare equal according to your sorting function may now have a different order.

    If you care about the sorting order of equal elements (and this is not just a testing issue), you should make it explicit in your comparison function.

提交回复
热议问题