使用返回不可预测的结果

Emm*_*myS 1 php sorting usort multidimensional-array

我有一个从数据库返回的对象数组.未排序数组上的print_r如下所示:

Array
(
    [0] => stdClass Object
        (
            [nid] => 53162
            [title] => Kroger 'Giving Hope' Standee
            [path] => node/53162
        )

    [1] => stdClass Object
        (
            [nid] => 64185
            [title] => Kroger 'Giving Hope' Stanchion Sign
            [path] => node/64185
        )

    [2] => stdClass Object
        (
            [nid] => 52190
            [title] => Betty Crocker Kroger 'Giving Hope' Shipper
            [path] => node/52190
        )

    [3] => stdClass Object
        (
            [nid] => 53159
            [title] => Frito-Lay Kroger 'Giving Hope' Packaging
            [path] => node/53159
        )

    [4] => stdClass Object
        (
            [nid] => 53164
            [title] => Nabisco Kroger 'Giving Hope' Violator
            [path] => node/53164
        )

    [5] => stdClass Object
        (
            [nid] => 52607
            [title] => Doritos Kroger 'Giving Hope' Packaging
            [path] => node/52607
        )

    [6] => stdClass Object
        (
            [nid] => 52720
            [title] => Kroger Big K Cola 'Giving Hope' Packaging
            [path] => node/52720
        )

    [7] => stdClass Object
        (
            [nid] => 52729
            [title] => Windex Kroger 'Giving Hope' Packaging
            [path] => node/52729
        )

    [8] => stdClass Object
        (
            [nid] => 52731
            [title] => Ziploc Kroger 'Giving Hope' Packaging
            [path] => node/52731
        )

    [9] => stdClass Object
        (
            [nid] => 53157
            [title] => Stacy's Kroger 'Giving Hope' Cut Cases
            [path] => node/53157
        )

)
Run Code Online (Sandbox Code Playgroud)

我想通过nid属性对数组进行排序,所以我使用了一个自定义的usort函数(在这里找到.)

function my_search_sort($a, $b) {
  strcmp($a->nid, $b->nid);
}
Run Code Online (Sandbox Code Playgroud)

我这样打电话:

usort($docs, 'my_search_sort');
Run Code Online (Sandbox Code Playgroud)

然后做了另一个print_r.结果如下所示:

Array
(
    [0] => stdClass Object
        (
            [nid] => 52720
            [title] => Kroger Big K Cola 'Giving Hope' Packaging
            [path] => node/52720
        )

    [1] => stdClass Object
        (
            [nid] => 52729
            [title] => Windex Kroger 'Giving Hope' Packaging
            [path] => node/52729
        )

    [2] => stdClass Object
        (
            [nid] => 52731
            [title] => Ziploc Kroger 'Giving Hope' Packaging
            [path] => node/52731
        )

    [3] => stdClass Object
        (
            [nid] => 53157
            [title] => Stacy's Kroger 'Giving Hope' Cut Cases
            [path] => node/53157
        )

    [4] => stdClass Object
        (
            [nid] => 52607
            [title] => Doritos Kroger 'Giving Hope' Packaging
            [path] => node/52607
        )

    [5] => stdClass Object
        (
            [nid] => 53164
            [title] => Nabisco Kroger 'Giving Hope' Violator
            [path] => node/53164
        )

    [6] => stdClass Object
        (
            [nid] => 64185
            [title] => Kroger 'Giving Hope' Stanchion Sign
            [path] => node/64185
        )

    [7] => stdClass Object
        (
            [nid] => 52190
            [title] => Betty Crocker Kroger 'Giving Hope' Shipper
            [path] => node/52190
        )

    [8] => stdClass Object
        (
            [nid] => 53159
            [title] => Frito-Lay Kroger 'Giving Hope' Packaging
            [path] => node/53159
        )

    [9] => stdClass Object
        (
            [nid] => 53162
            [title] => Kroger 'Giving Hope' Standee
            [path] => node/53162
        )

)
Run Code Online (Sandbox Code Playgroud)

它显然在做某些事情,但根据nid,它并没有按升序排序.我需要做些什么才能让它发挥作用?

Cli*_*ive 5

您的自定义排序函数需要返回值:

function my_search_sort($a, $b) {
  return strcmp($a->nid, $b->nid);
}
Run Code Online (Sandbox Code Playgroud)

编辑 我已经更新了您引用的其他答案的代码,这是不正确的.