• Mysql 递归获取多重数组数据


    多重数据 $data ,获取顶级下的所有下级id

    $data 

    array:3 [▼
      0 => array:7 [▼
        "id" => 1
        "created_at" => "2017-08-05 11:30:15"
        "updated_at" => "2017-08-05 11:30:15"
        "parent_id" => 0
        "children" => array:1 [▼
          0 => array:7 [▼
            "id" => 2
            "created_at" => "2017-08-05 11:31:11"
            "updated_at" => "2017-08-18 18:57:11"
            "parent_id" => 1
            "children" => array:1 [▼
              0 => array:7 [▼
                "id" => 3
                "created_at" => "2017-08-05 11:33:34"
                "updated_at" => "2017-08-05 11:33:34"
                "parent_id" => 2
                "children" => []
              ]
            ]
          ]
        ]
      ]
      1 => array:7 [▼
        "id" => 4
        "created_at" => "2017-08-10 15:36:03"
        "updated_at" => "2017-08-10 15:36:03"
        "parent_id" => 0
        "children" => array:1 [▼
          0 => array:7 [▼
            "id" => 5
            "created_at" => "2017-08-10 15:36:31"
            "updated_at" => "2017-08-10 15:42:08"
            "parent_id" => 4
            "children" => []
          ]
        ]
      ]
      2 => array:7 [▼
        "id" => 7
        "created_at" => "2017-08-10 15:47:09"
        "updated_at" => "2017-08-10 15:50:37"
        "parent_id" => 0
        "children" => array:1 [▼
          0 => array:7 [▼
            "id" => 8
            "created_at" => "2017-08-10 15:47:20"
            "updated_at" => "2017-08-10 15:50:48"
            "parent_id" => 7
            "children" => array:1 [

           0 => array:7 [
                "id" => 9
                "created_at" => "2017-08-10 15:49:17"
                "updated_at" => "2017-08-10 15:51:08"
                "parent_id" => 8
                "children" => []
              ]
            ]
    ] ] ] ]

    创建一个方法

     1     public function getCategoryChildrenIds($data, $lev = 0)
     2     {
     3         $arr = [];
     4         foreach ($data as $key => $value) {
     5             if ($lev) {
     6                 $arr[] = $value->id;
     7             }
     8 
     9             if( isset($value['children'])) {
    10                 $childLev = $lev + 1;
    11                 $arr = array_merge($arr, self::getCategoryChildrenIds($value['children'], $childLev));
    12 
    13             }
    14 
    15         }
    16         return $arr;
    17     }

    获取顶级下所有下级id

    array:5 [▼
      0 => 2
      1 => 3
      2 => 5
      3 => 8
      4 => 9
    ]
  • 相关阅读:
    一道leetcode题的收获如何比较字符串的大小重写sort中的compare[](string &s,string &t){return s+t>t+s};
    unsigned int表示负数问题
    fork()和printf()几点注意细节
    32位机中数据问题
    C++隐藏机制
    ||,&&,++i解答
    enum忽略知识点
    硬链接与软链接
    20145215实验五 Java网络编程及安全
    证书与keytool
  • 原文地址:https://www.cnblogs.com/smallyi/p/7421719.html
Copyright © 2020-2023  润新知