按子数组的数量对数组进行排序

2023-11-25

我有一个看起来像这样的数组:

Array(
   ['some_first_category'] => Array(
            ['some_first_name'] => Array(
                           [0]=>'[email protected]',
                           [1]=>'[email protected]',
                           [2]=>'[email protected]',
                           [3]=>'[email protected]' )
             ['some_second_name'] => Array (
                           [1]=>'[email protected]',
                           [2]=>'[email protected]')
             ['some_third_name'] => Array(
                           [1]=>'[email protected]',
                           [2]=>'[email protected]',
                           [3]=>'[email protected]',
                           [4]=>'[email protected]' )
   ['some_second_category'] => Array(
            ['some_first_name'] => Array(
                           [0]=>'[email protected]' )
             ['some_second_name'] => Array(
                           [1]=>'[email protected]',
                           [2]=>'[email protected]',
                           [3]=>'[email protected]',
                           [4]=>'[email protected]')
             ['some_third_name'] => Array(
                           [1]=>'[email protected]',
                           [2]=>'[email protected]'))

我想按具有名称的值的数量对数组进行排序,就我而言,我想成为这个数组:

Array(
   ['some_first_category'] => Array(
             ['some_third_name'] => Array(
                           [1]=>'[email protected]',
                           [2]=>'[email protected]',
                           [3]=>'[email protected]',
                           [4]=>'[email protected]' )
            ['some_first_name'] => Array(
                           [0]=>'[email protected]',
                           [1]=>'[email protected]',
                           [2]=>'[email protected]',
                           [3]=>'[email protected]' )
             ['some_second_name'] => Array (
                           [1]=>'[email protected]',
                           [2]=>'[email protected]')

   ['some_second_category'] => Array(
             ['some_second_name'] => Array(
                           [1]=>'[email protected]',
                           [2]=>'[email protected]',
                           [3]=>'[email protected]',
                           [4]=>'[email protected]')
             ['some_third_name'] => Array(
                           [1]=>'[email protected]',
                           [2]=>'[email protected]')
            ['some_first_name'] => Array(
                           [0]=>'[email protected]' ))

这意味着按名称值的数量(计数)对类别进行排序。有人可以帮助我吗? 提前致谢,

Aäron


所有你需要的是uasort

uasort($list, function ($a, $b) {
    $a = count($a);
    $b = count($b);
    return ($a == $b) ? 0 : (($a < $b) ? -1 : 1);
});

完整示例

$list = Array(
   'some_first_category' => Array(
            'some_first_name' => Array(
                           0=>'[email protected]',
                           1=>'secon[email protected]',
                           2=>'[email protected]',
                           3=>'[email protected]' ),
             'some_second_name' => Array (
                           1=>'[email protected]',
                           2=>'[email protected]'),
             'some_third_name' => Array(
                           1=>'[email protected]',
                           2=>'[email protected]',
                           3=>'[email protected]',
                           4=>'[email protected]' )
        ),
   'some_second_category' => Array(
            'some_first_name' => Array(
                           0=>'[email protected]' ),
             'some_second_name' => Array(
                           1=>'[email protected]',
                           2=>'[email protected]',
                           3=>'[email protected]',
                           4=>'[email protected]'),
             'some_third_name' => Array(
                           1=>'[email protected]',
                           2=>'[email protected]'))

    );

$list = array_map(function ($v) {
    uasort($v, function ($a, $b) {
        $a = count($a);
        $b = count($b);
        return ($a == $b) ? 0 : (($a < $b) ? 1 : - 1);
    });
    return $v;
}, $list);


print_r($list);

Output

Array
(
    [some_first_category] => Array
        (
            [some_first_name] => Array
                (
                    [0] => [email protected]
                    [1] => [email protected]
                    [2] => [email protected]
                    [3] => [email protected]
                )

            [some_third_name] => Array
                (
                    [1] => [email protected]
                    [2] => [email protected]
                    [3] => [email protected]
                    [4] => [email protected]
                )

            [some_second_name] => Array
                (
                    [1] => [email protected]
                    [2] => [email protected]
                )

        )

    [some_second_category] => Array
        (
            [some_second_name] => Array
                (
                    [1] => [email protected]
                    [2] => seco[email protected]
                    [3] => [email protected]
                    [4] => [email protected]
                )

            [some_third_name] => Array
                (
                    [1] => [email protected]
                    [2] => [email protected]
                )

            [some_first_name] => Array
                (
                    [0] => [email protected]
                )

        )

)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

按子数组的数量对数组进行排序 的相关文章

随机推荐

  • 如何强制 go mod 接受声明其路径与其 go.mod 不同的模块?

    当我跑步时go mod tidy 它破坏了 因为我的项目导入的包使用路径导入了另一个包github com coreos bbolt 但是当它从此路径获取包时go mod说它的路径是go etcd io bbolt 问题是导入包和导入包都是
  • 如何以编程方式读取 MSI 文件内的属性?

    有没有办法读取里面的属性MSI file 例如 给定一个名为测试包 msi 我需要找到 productName PackageCode version 我将把它与 WMI 卸载一起使用 string objPath string Forma
  • 为什么在放弃泛型运算符时无法推断类型[重复]

    这个问题在这里已经有答案了 我读到 从 Java 7 开始 像第一个语句一样在右侧指定类型来创建集合是不好的风格 因为编译器可以从左侧推断类型 List
  • 方形拼图解决方案

    Question given an integer number n print the numbers from 1 up to n2 like this n 4 结果是 01 02 03 04 12 13 14 05 11 16 15
  • 函数调用中的 C++ 临时对象生命周期

    当我们通过原始指针或引用将临时智能指针管理的对象传递给函数时 标准是否保证该对象的生存期将延长到函数的生存期 include
  • JQuery JEditable - 如何禁用点击编辑

    我想知道您是否可以停止点击时编辑文本 我有一个单独的编辑按钮来使文本可编辑 这是我希望用户能够编辑文本的唯一方法 因此想要关闭单击编辑吗 有任何想法吗 布里特是对的 添加一个自定义事件 然后用按钮触发它 这是一些代码来解释它 自定义事件 i
  • 使用 N-API 将数据流式传输到 Node.js C++ 插件

    我正在为 NodeJS 构建一个 C 插件 我想将数据从 C 异步流式传输到 Node 我找到了这篇文章 https nodeaddons com streaming data into a node js c addon 然而 我想使用
  • Django:HttpResponseRedirect 不起作用

    我是 Python Django 和整体编程的新手 我需要有关 HttpResponseRedirect 的帮助 因为它在我的登录视图中不起作用 它确实可以在我的主视图文件中工作 但不是我想要的方式 我没有重定向到所需的页面 而是只在同一页
  • 如何在API平台上保存与实体的嵌套关系

    我有两个实体 Question and 选择其中 Question 与 Alternative 有 OneToMany 关系 我正在尝试发送带有嵌套文档的 JSON选择通过 POST 到QuestionAPI 平台 API 平台返回以下错误
  • 如何在 Dart 中逐行读取文件

    这个问题是一个问题的延续上一个问题 我编写了以下代码来确定是否File openRead 创建了一个可以逐行流式传输的 Stream 事实证明答案是否定的 读取整个文件 然后传递到下一个转换 我的问题是 如何在 Dart 中逐行流式传输文件
  • 如何在不复制数据的情况下连接 pandas DataFrame?

    我想连接两个 pandas DataFrame 而不复制数据 也就是说 我希望连接的 DataFrame 成为两个原始 DataFrame 中数据的视图 我尝试使用 concat 但不起作用 此代码块显示更改基础数据会影响连接的两个 Dat
  • 从 SQLite3 导出数据

    我需要一种简单的方法从包含多个表的 SQLite 数据库中导出数据 然后将它们导入到另一个数据库中 这是我的场景 我有 5 个表 A B C D E 每个表都有一个主键作为第一列 称为 ID 我想要一个 Unix 命令 它仅以可以导入到另一
  • 如何反序列化 Kubernetes YAML 文件

    如何将 Kubernetes YAML 文件反序列化为 Go 结构 我查看了kubectl代码 但不知何故 每个 YAML 文件都会出现错误 no kind Deployment is registered for version apps
  • Terraform EKS 标记

    我遇到了 Terraform EKS 标记问题 并且似乎没有找到可行的解决方案来在创建新集群时标记所有 VPC 子网 提供一些背景信息 我们有一个 AWS VPC 在其中将多个 EKS 集群部署到子网中 我们不会在 EKS 集群创建过程中创
  • FirebaseError:[code=resource-exhausted]:资源已耗尽(例如检查配额)

    我有一个大小为 10000 的数组 所有这些都是文档 ID 我正在运行一个数组 需要从 Firestore 获取文档数据 并且需要在每个文档中添加新字段 但我面临以下错误 例如 firebase firestore Firestore 5
  • DATETIME 值在 SQLite 中如何工作?

    我正在创建 Android 应用程序 需要保存创建记录的日期 时间 然而 SQLite 文档说 SQLite 没有为存储日期和 或时间而预留的存储类 并且它 能够将日期和时间存储为 TEXT REAL 或 INTEGER 值 使用一种类型而
  • PHP 中有比任何数字都大的东西吗?

    我需要在 PHP 中模拟 So that min number 总是 number 我想 对于整数 你可以使用PHP INT MAX 以下代码 var dump PHP INT MAX 在我的机器上给出以下输出 int 2147483647
  • AccessType.FIELD、AccessType.PROPERTY 和 @Access 的用途是什么

    我只是想知道所有这些注释之间有什么区别 为什么我们使用这些 意味着它们没有影响 尤其是字段级别和属性级别 使用混合级别注释的目的是什么 Entity Access AccessType FIELD class Employee why th
  • C 中是否有单消费者单生产者无锁队列实现?

    我正在编写一个带有消费者线程和生产者线程的程序 现在看来队列同步在程序中是一个很大的开销 我寻找了一些无锁队列实现 但只找到了Lamport的版本和PPoPP上的改进版本 08 enqueue nonblock data if NULL b
  • 按子数组的数量对数组进行排序

    我有一个看起来像这样的数组 Array some first category gt Array some first name gt Array 0 gt email protected 1 gt email protected 2 gt