在数组之后对 stdClass 对象进行排序

发布于 2024-11-25 23:20:10 字数 1046 浏览 2 评论 0原文

我有两个数组,

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [title] => art
        )
    [1] => stdClass Object
        (
            [id] => 4
            [title] => adsdf
        )
    [2] => stdClass Object
        (
            [id] => 2
            [title] => adsdf
        )
    [3] => stdClass Object
        (
            [id] => 7
            [title] => adsdf
        )

)
Array
(
    [2] => 2
    [1] => 1
)

我想在第二个数组之后对第一个数组进行排序。在第二个数组中,键和值与第一个数组的 id 相等。所以输出必须如下。

Array
(
    [0] => stdClass Object
        (
            [id] => 2
            [title] => adsdf
        )
    [1] => stdClass Object
        (
            [id] => 1
            [title] => art
        )
    [2] => stdClass Object
        (
            [id] => 4
            [title] => adsdf
        )
    [3] => stdClass Object
        (
            [id] => 7
            [title] => adsdf
        )

)

I have two arrays,

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [title] => art
        )
    [1] => stdClass Object
        (
            [id] => 4
            [title] => adsdf
        )
    [2] => stdClass Object
        (
            [id] => 2
            [title] => adsdf
        )
    [3] => stdClass Object
        (
            [id] => 7
            [title] => adsdf
        )

)
Array
(
    [2] => 2
    [1] => 1
)

And I want to sort the first array after the second array. In the second array the key and the value is equal with the first array id. So the output have to be the following.

Array
(
    [0] => stdClass Object
        (
            [id] => 2
            [title] => adsdf
        )
    [1] => stdClass Object
        (
            [id] => 1
            [title] => art
        )
    [2] => stdClass Object
        (
            [id] => 4
            [title] => adsdf
        )
    [3] => stdClass Object
        (
            [id] => 7
            [title] => adsdf
        )

)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴我老 2024-12-02 23:20:10

您可以使用 array_multisort[文档]

array_multisort($arraySort, $arrayData);

将排序顺序作为第一个参数传递数组,将要排序的数组作为第二个参数传递。

您可能需要在它之前构建排序数组,从您的问题来看,我不清楚您是否已经拥有它。

如果不是,如果您想将所有数据数组条目 ID 值放入排序数组中:

$arraySort = array();
foreach($arrayData as $key => $obj)
{
    $arraySort[$key] = $obj->id;
}

You can use array_multisort[Docs] for it:

array_multisort($arraySort, $arrayData);

Pass the array with the sort order as the first and your array to be sorted as the second parameter.

You might need to build the sort array prior to it, from your question it's not clear to me if you already have it or not.

In case not, if you want to get all of the data arrays entries ID values into the sort array:

$arraySort = array();
foreach($arrayData as $key => $obj)
{
    $arraySort[$key] = $obj->id;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文