根据多维数组中的键对数组进行排序并保留其他键值。 PHP

发布于 2024-12-09 22:19:01 字数 1233 浏览 0 评论 0原文

这是我的阵列。

Array
        (
            [Data] => Array
                (
                    [0] => Array
                        (
                            [recipeid] => 108
                            [recipe] => Rasams- the tongue ticklers ! 
                            [image] => No data
                            [category] => Rasams and Soups
                        )

                    [1] => Array
                        (
                            [recipeid] => 44
                            [recipe] => Brain Booster- do you want to try it?
                            [image] => brain-booster-do-you-44-HP-62.jpg
                            [category] => Drinks and Smoothies
                        )

                    [2] => Array
                        (
                            [recipeid] => 36
                            [recipe] => Pineapple Grape Smoothy--a rare combo
                            [image] => pineapple-grape-smoo-36-HP-62.jpg
                            [category] => Drinks and Smoothies
                        )

                )

        )

我必须根据[key]recipe值的字母顺序对[DATA]数组进行排序,并在排序后保留recipeid、图像、类别。

This is my array.

Array
        (
            [Data] => Array
                (
                    [0] => Array
                        (
                            [recipeid] => 108
                            [recipe] => Rasams- the tongue ticklers ! 
                            [image] => No data
                            [category] => Rasams and Soups
                        )

                    [1] => Array
                        (
                            [recipeid] => 44
                            [recipe] => Brain Booster- do you want to try it?
                            [image] => brain-booster-do-you-44-HP-62.jpg
                            [category] => Drinks and Smoothies
                        )

                    [2] => Array
                        (
                            [recipeid] => 36
                            [recipe] => Pineapple Grape Smoothy--a rare combo
                            [image] => pineapple-grape-smoo-36-HP-62.jpg
                            [category] => Drinks and Smoothies
                        )

                )

        )

I have to sort [DATA] array according to alphabetical order of [key]recipe's value, also preserve the recipeid, image, category after sorting.

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

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

发布评论

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

评论(2

森林很绿却致人迷途 2024-12-16 22:19:02

使用usort。

usort($yourarray['Data'], 'data_sort');

function data_sort($a, $b) {
  return (strcasecmp($a['recipe'], $b['recipe']) > 0);
}

Use usort.

usort($yourarray['Data'], 'data_sort');

function data_sort($a, $b) {
  return (strcasecmp($a['recipe'], $b['recipe']) > 0);
}
倾城泪 2024-12-16 22:19:02

您应该可以使用 usort() 来完成此操作。下面是一个基于 PHP 文档中给出的示例的示例,说明如何完成此操作。

function cmp($a, $b)
{
    if ($a['recipe'] == $b['recipe']) {
        return 0;
    }
    return ($a['recipe'] < $b['recipe']) ? -1 : 1;
}

usort($a, "cmp");

You should be able to do this with usort(). Here's an example of how it could be done, based off of the example given in the PHP docs.

function cmp($a, $b)
{
    if ($a['recipe'] == $b['recipe']) {
        return 0;
    }
    return ($a['recipe'] < $b['recipe']) ? -1 : 1;
}

usort($a, "cmp");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文