亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長(zhǎng)資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    PHP使用遞歸按層級(jí)查找數(shù)據(jù)(代碼詳解)

    PHP使用遞歸按層級(jí)查找數(shù)據(jù)(代碼詳解)

    今天主要介紹一下使用遞歸來按層級(jí)查找數(shù)據(jù)。

    原理挺簡(jiǎn)單的,主要是通過父級(jí)id一級(jí)一級(jí)的循環(huán)查找子級(jí),使用PHP循環(huán)代碼也很容易實(shí)現(xiàn),不過如果層級(jí)越多,PHP重復(fù)代碼也越多,這時(shí)可以使用遞歸來實(shí)現(xiàn)這功能。

    1、首先查出要使用的數(shù)據(jù)組成一個(gè)數(shù)組(避免遞歸里查詢數(shù)據(jù)庫,之后根據(jù)這個(gè)數(shù)組組成自己需要的數(shù)據(jù)就可以了)

    比如得到如下數(shù)據(jù):

    $data = [     ['id' => '1', 'pid' => '0', 'dsp' => '1'],     ['id' => '2', 'pid' => '0', 'dsp' => '2'],     ['id' => '3', 'pid' => '0', 'dsp' => '3'],     ['id' => '4', 'pid' => '1', 'dsp' => '1-4'],     ['id' => '5', 'pid' => '4', 'dsp' => '1-4-5'],     ['id' => '6', 'pid' => '5', 'dsp' => '1-4-5-6'],     ['id' => '7', 'pid' => '3', 'dsp' => '3-7'],     ['id' => '8', 'pid' => '2', 'dsp' => '2-8'],     ['id' => '9', 'pid' => '1', 'dsp' => '1-9'],     ['id' => '10', 'pid' => '4', 'dsp' => '1-4-10'], ];

    2、接下來使用遞歸重組數(shù)據(jù),使數(shù)據(jù)按層級(jí)顯示。

    /**  * 根據(jù)父級(jí)id查找子級(jí)數(shù)據(jù)  * @param $data     要查詢的數(shù)據(jù)  * @param int $pid 父級(jí)id  */ public function recursion($data, $pid = 0) {     static $child = [];   // 定義存儲(chǔ)子級(jí)數(shù)據(jù)數(shù)組     foreach ($data as $key => $value) {         if ($value['pid'] == $pid) {             $child[] = $value;   // 滿足條件的數(shù)據(jù)添加進(jìn)child數(shù)組             unset($data[$key]);  // 使用過后可以銷毀             $this->recursion($data, $value['id']);   // 遞歸調(diào)用,查找當(dāng)前數(shù)據(jù)的子級(jí)         }     }     return $child; }

    得到結(jié)果:

    [   {     "id": "1",     "pid": "0",     "dsp": "1"   },   {     "id": "4",     "pid": "1",     "dsp": "1-4"   },   {     "id": "5",     "pid": "4",     "dsp": "1-4-5"   },   {     "id": "6",     "pid": "5",     "dsp": "1-4-5-6"   },   {     "id": "10",     "pid": "4",     "dsp": "1-4-10"   },   {     "id": "9",     "pid": "1",     "dsp": "1-9"   },   {     "id": "2",     "pid": "0",     "dsp": "2"   },   {     "id": "8",     "pid": "2",     "dsp": "2-8"   },   {     "id": "3",     "pid": "0",     "dsp": "3"   },   {     "id": "7",     "pid": "3",     "dsp": "3-7"   } ]

    3、還可以使用下面的方法,顯示更有層次感。

    /**  * 根據(jù)父級(jí)id查找子級(jí)數(shù)據(jù)  * @param $data     要查詢的數(shù)據(jù)  * @param int $pid 父級(jí)id  */ public function recursion($data, $pid = 0) {     $child = [];   // 定義存儲(chǔ)子級(jí)數(shù)據(jù)數(shù)組     foreach ($data as $key => $value) {         if ($value['pid'] == $pid) {             unset($data[$key]);  // 使用過后可以銷毀             $value['child'] = $this->recursion($data, $value['id']);   // 遞歸調(diào)用,查找當(dāng)前數(shù)據(jù)的子級(jí)             $child[] = $value;   // 把子級(jí)數(shù)據(jù)添加進(jìn)數(shù)組         }     }     return $child; }

    得到結(jié)果:

    [   {     "id": "1",     "pid": "0",     "dsp": "1",     "child": [       {         "id": "4",         "pid": "1",         "dsp": "1-4",         "child": [           {             "id": "5",             "pid": "4",             "dsp": "1-4-5",             "child": [               {                 "id": "6",                 "pid": "5",                 "dsp": "1-4-5-6",                 "child": []               }             ]           },           {             "id": "10",             "pid": "4",             "dsp": "1-4-10",             "child": []           }         ]       },       {         "id": "9",         "pid": "1",         "dsp": "1-9",         "child": []       }     ]   },   {     "id": "2",     "pid": "0",     "dsp": "2",     "child": [       {         "id": "8",         "pid": "2",         "dsp": "2-8",         "child": []       }     ]   },   {     "id": "3",     "pid": "0",     "dsp": "3",     "child": [       {         "id": "7",         "pid": "3",         "dsp": "3-7",         "child": []       }     ]   } ]

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)