PHP練習題之:使用匿名函數遞歸調用(閉包)

PHP閉包官方的定義:

匿名函數(Anonymous functions),也叫閉包函數(closures),允許 臨時創建一個沒有指定名稱的函數。最經常用作回調函數(callback)參數的值。當然,也有其它應用的情況。

比如在項目中,需要處理具有遞歸特徵的數據報文,

以下示例,JSON數組對象每一個節點爲一個產品數據,產品數據可能包含了不同類型的關聯產品(示例JSON中的relation_item字段)或關聯配件(示例JSON中的relation_part字段),關聯產品或配件其實也是一個完整的產品數據結構,

<?php

//JSON數據報文,底部有提供測試數據
$sResponse = '';

$aResponse = json_decode($sResponse, true);

$readCache = true;

defined('MAPI_IMG_HOST') OR define('MAPI_IMG_HOST', 'http://api.local');

$defaultThumbs = [
    's' => MAPI_IMG_HOST . "/upload/main/default.png",
    'i' => MAPI_IMG_HOST . "/upload/main/default.png",
    'm' => MAPI_IMG_HOST . "/upload/main/default.png",
];

//業務代碼可以忽略
//定義處理數據的匿名函數,因爲需要遞歸調用自己,因此$func做爲參數要放到use裏面
$func = function($val) use ($readCache, $defaultThumbs, &$func) {
    
    $skuData = [];
    $relationItem = [];
    $relationPart = [];
    //$downImageRetval = $this->downImage($val['productmainimage'], $readCache);
    
    $baseInfo = [
        'type_name' => '',
        'brand_pid' => $val['productbrandpid'],
        'brand_name' => $val['productbrandname'],
        'model_name' => $val['productmodelname'],
        'image' => MAPI_IMG_HOST . $val['productmainimage'],
        'image_thum' => $downImageRetval['s'] ?? $defaultThumbs['s'],
        'image_thum_i' => $downImageRetval['i'] ?? $defaultThumbs['i'],
        'image_thum_m' => $downImageRetval['m'] ?? $defaultThumbs['m'],
        'desc' => $val['productsummary'],
        'unit' => $val['productunit'],
    ];
    
    if ($val['children']) {
        foreach ($val['children'] as $k => $v) {
            $skuData[] = [
                'id' => $v['pid'],
                'pid' => $val['pid'],
                'label' => $val['productname'],
                'item' => $v['productcoloritem'],
                'price' => $v['productsalesprice'] ?? $v['productcolorprice'],
                'low_price' => $v['productlowprice'] ?? 0,
                'cost_price' => $v['productcostprice'] ?? 0,
                'base' => $baseInfo
            ];
        }
    } else {
        $skuData[] = [
            'id' => $val['pid'] . '99',
            'pid' => $val['pid'],
            'label' => $val['productname'],
            'item' => '默認規格',
            'price' => 0,
            'low_price' => 0,
            'cost_price' => 0,
            'base' => $baseInfo
        ];
    }
    
    //如果relation_item有數據,就產生了遞歸,需要$func調用自己來解析數據
    if (isset($val['relation_item']) && $val['relation_item']) {
        foreach ($val['relation_item'] as $item) {
            array_push($relationItem, $func($item));
        }
    }
    //遞歸處理relation_part
    if (isset($val['relation_part']) && $val['relation_part']) {
        foreach ($val['relation_part'] as $part) {
            array_push($relationPart, $func($part));
        }
    }

    $aProduct = [
        'pid' => $val['pid'],
        'name' => $val['productname'],
        'brand' => $val['productbrandname'],
        'model' => $val['productmodelname'],
        'desc' => $val['productsummary'],
        'image' => MAPI_IMG_HOST . $val['productmainimage'],
        'image_thum' => $downImageRetval['s'] ?? $defaultThumbs['s'],
        'image_thum_i' => $downImageRetval['i'] ?? $defaultThumbs['i'],
        'image_thum_m' => $downImageRetval['m'] ?? $defaultThumbs['m'],
        'sku' => $skuData,
        'relation_item' => $relationItem,
        'relation_part' => $relationPart,
    ];
    
    return $aProduct;
};

$aProducts = [];
//數據處理入口,調用$func處理報文數據
foreach ($aResponse['result']['data'] as $key => $val) {
    array_push($aProducts, $func($val));
}

//打印處理後的結果
var_dump($aProducts);
{
    "resultCode":"SUCCESS",
    "result":{
        "total":835,
        "data":[
            {
                "productbrandpid":"1038518770353471488",
                "relation_part":[
                    {
                        "productbrandpid":"4941513789561274368",
                        "productsummary":"阻抗:8Ω 靈敏度:90DB。///顏色:白色 開孔尺寸:180mm。///重量:.1.8kg。///功率:25W(本機)+25W(外接)。///頻響範圍:20-22000Hz(±3DB)。///包裝尺寸:253*253*143mm。///外觀尺寸:195mm(直徑)100mm(深度)。
",
                        "productbrandname":"Wise/華爾思",
                        "productunit":"只",
                        "children":[
                            {
                                "productsalesprice":"0",
                                "productcoloritem":"2",
                                "productstock":0,
                                "productmainimage":"",
                                "pid":"1038258069059829760",
                                "productlowprice":"0",
                                "productcostprice":"0",
                                "marketprice":0
                            }
                        ],
                        "productmainimage":"/maincorp/uploadfile/productimage/164_164/1979267431075149824.jpg",
                        "productname":"5寸吸頂式背景音樂喇叭",
                        "productmodelname":"WS-TPZ409",
                        "pid":"1115920293697585152",
                        "accessoriesnum":1
                    },
                    {
                        "productbrandpid":"4941513789561274368",
                        "productsummary":"阻抗:8Ω 靈敏度:90±2DB。///顏色:白色 開孔尺寸:165mm。///功率:10W 重量:600g。///頻響範圍:90-20KHz。///包裝尺寸:193*193*83mm。///外觀尺寸:189(直徑)70mm(深度)。
",
                        "productbrandname":"Wise/華爾思",
                        "productunit":"只",
                        "children":[
                            {
                                "productsalesprice":"0",
                                "productcoloritem":"2",
                                "productstock":0,
                                "productmainimage":"",
                                "pid":"1038258069059829760",
                                "productlowprice":"0",
                                "productcostprice":"0",
                                "marketprice":0
                            }
                        ],
                        "productmainimage":"/maincorp/uploadfile/productimage/164_164/1876440987103434752.jpg",
                        "productname":"6.5寸吸頂式背景音樂喇叭",
                        "productmodelname":"WS-TPZ403 ",
                        "pid":"1115927482399096832",
                        "accessoriesnum":1
                    },
                    {
                        "productbrandpid":"4941513789561274368",
                        "productsummary":"阻抗:8Ω 靈敏度:91DB。///顏色:白色 開孔尺寸:160mm。///功率:25W 重量:700g。///頻響範圍:50-22000Hz。///包裝尺寸:250*250*110mm。///外觀尺寸:180mm(直徑)68mm(深度)。
",
                        "productbrandname":"Wise/華爾思",
                        "productunit":"只",
                        "children":[
                            {
                                "productsalesprice":"0",
                                "productcoloritem":"2",
                                "productstock":0,
                                "productmainimage":"",
                                "pid":"1038258069059829760",
                                "productlowprice":"0",
                                "productcostprice":"0",
                                "marketprice":0
                            }
                        ],
                        "productmainimage":"/maincorp/uploadfile/productimage/164_164/5768153958271583232.jpg",
                        "productname":"4寸吸頂式背景音樂喇叭",
                        "productmodelname":"WS-TPZ405 ",
                        "pid":"1115932198273187840",
                        "accessoriesnum":1
                    }
                ],
                "productsummary":"面板材質爲金屬
> 可作爲燈控門窗開關和場景開關,聯動場景
> 帶狀態反饋和指示燈以及背光
> 指示燈顏色可根據情景而改變
> 按鍵字體可單獨定製(收費)
> 邊框顏色可選黑色、灰色",
                "productbrandname":"AIKS/埃克思",
                "productunit":"塊",
                "children":[
                    {
                        "productsalesprice":"0",
                        "productcoloritem":"2",
                        "productstock":0,
                        "productmainimage":"",
                        "pid":"1038258069059829760",
                        "productlowprice":"0",
                        "productcostprice":"0",
                        "marketprice":0
                    }
                ],
                "relation_item":[
                    {
                        "productbrandpid":"4941513789561274368",
                        "productsummary":"阻抗:8Ω 靈敏度:90±2DB。///顏色:白色 開孔尺寸:165mm。///功率:10W 重量:600g。///頻響範圍:90-20KHz。///包裝尺寸:193*193*83mm。///外觀尺寸:189(直徑)70mm(深度)。
",
                        "productbrandname":"Wise/華爾思",
                        "productunit":"只",
                        "children":[
                            {
                                "productsalesprice":"0",
                                "productcoloritem":"2",
                                "productstock":0,
                                "productmainimage":"",
                                "pid":"1038258069059829760",
                                "productlowprice":"0",
                                "productcostprice":"0",
                                "marketprice":0
                            }
                        ],
                        "productmainimage":"/maincorp/uploadfile/productimage/164_164/1876440987103434752.jpg",
                        "productname":"6.5寸吸頂式背景音樂喇叭",
                        "productmodelname":"WS-TPZ403 ",
                        "pid":"1115927482399096832"
                    },
                    {
                        "productbrandpid":"4941513789561274368",
                        "productsummary":"阻抗:8Ω 靈敏度:91DB。///顏色:白色 開孔尺寸:160mm。///功率:25W 重量:700g。///頻響範圍:50-22000Hz。///包裝尺寸:250*250*110mm。///外觀尺寸:180mm(直徑)68mm(深度)。
",
                        "productbrandname":"Wise/華爾思",
                        "productunit":"只",
                        "children":[
                            {
                                "productsalesprice":"0",
                                "productcoloritem":"2",
                                "productstock":0,
                                "productmainimage":"",
                                "pid":"1038258069059829760",
                                "productlowprice":"0",
                                "productcostprice":"0",
                                "marketprice":0
                            }
                        ],
                        "productmainimage":"/maincorp/uploadfile/productimage/164_164/5768153958271583232.jpg",
                        "productname":"4寸吸頂式背景音樂喇叭",
                        "productmodelname":"WS-TPZ405 ",
                        "pid":"1115932198273187840"
                    }
                ],
                "productmainimage":"/maincorp/uploadfile/productimage/20200211/164_164/991330150966525952.jpg",
                "productname":"1開F系列總線開關面板(金屬)",
                "productmodelname":"TPN-F/M1",
                "pid":"1038258083555344384"
            },
            {
                "productbrandpid":"1038518770353471488",
                "relation_part":[

                ],
                "productsummary":"面板材質爲塑料
> 可作爲燈控門窗開關和場景開關,聯動場景
> 帶狀態反饋和指示燈以及背光
> 指示燈顏色可根據情景而改變
> 按鍵字體可單獨定製(收費)
> 邊框顏色可選黑色、灰色",
                "productbrandname":"AIKS/埃克思",
                "productunit":"塊",
                "children":[
                    {
                        "productsalesprice":"0",
                        "productcoloritem":"2",
                        "productstock":0,
                        "productmainimage":"",
                        "pid":"1038258106103922688",
                        "productlowprice":"0",
                        "productcostprice":"0",
                        "marketprice":0
                    }
                ],
                "relation_item":[

                ],
                "productmainimage":"/maincorp/uploadfile/productimage/20200211/164_164/991448351150866432.jpg",
                "productname":"6開F系列總線開關面板(塑料)",
                "productmodelname":"TPN-F/S6",
                "pid":"1038258119525695488"
            },
            {
                "productbrandpid":"1038518770353471488",
                "relation_part":[

                ],
                "productsummary":"> 面板材質爲塑料
> 可作爲燈控門窗開關和場景開關,聯動場景
> 帶狀態反饋和指示燈以及背光
> 指示燈顏色可根據情景而改變
> 按鍵字體可單獨定製(收費)
> 邊框顏色可選黑色、灰色",
                "productbrandname":"AIKS/埃克思",
                "productunit":"塊",
                "children":[
                    {
                        "productsalesprice":"0",
                        "productcoloritem":"2",
                        "productstock":0,
                        "productmainimage":"",
                        "pid":"1038258142611144704",
                        "productlowprice":"0",
                        "productcostprice":"0",
                        "marketprice":0
                    }
                ],
                "relation_item":[

                ],
                "productmainimage":"/maincorp/uploadfile/productimage/20200211/164_164/991371421844144128.jpg",
                "productname":"4開F系列總線開關面板(塑料)",
                "productmodelname":"TPN-F/S4",
                "pid":"1038258156569788416"
            },
            {
                "productbrandpid":"1038518770353471488",
                "relation_part":[

                ],
                "productsummary":"> 面板材質爲塑料
> 可作爲燈控門窗開關和場景開關,聯動場景
> 帶狀態反饋和指示燈以及背光
> 指示燈顏色可根據情景而改變
> 按鍵字體可單獨定製(收費)
> 邊框顏色可選黑色、灰色",
                "productbrandname":"AIKS/埃克思",
                "productunit":"塊",
                "children":[
                    {
                        "productsalesprice":"0",
                        "productcoloritem":"2",
                        "productstock":0,
                        "productmainimage":"",
                        "pid":"1038258179655237632",
                        "productlowprice":"0",
                        "productcostprice":"0",
                        "marketprice":0
                    }
                ],
                "relation_item":[

                ],
                "productmainimage":"/maincorp/uploadfile/productimage/20200211/164_164/991377062209945600.jpg",
                "productname":"3開F系列總線開關面板(塑料)",
                "productmodelname":"TPN-F/S3",
                "pid":"1038258193613881344"
            },
            {
                "productbrandpid":"1038518770353471488",
                "relation_part":[

                ],
                "productsummary":"> 面板材質爲塑料
> 可作爲燈控門窗開關和場景開關,聯動場景
> 帶狀態反饋和指示燈以及背光
> 指示燈顏色可根據情景而改變
> 按鍵字體可單獨定製(收費)
> 邊框顏色可選黑色、灰色",
                "productbrandname":"AIKS/埃克思",
                "productunit":"塊",
                "children":[
                    {
                        "productsalesprice":"0",
                        "productcoloritem":"2",
                        "productstock":0,
                        "productmainimage":"",
                        "pid":"1038258216162459648",
                        "productlowprice":"0",
                        "productcostprice":"0",
                        "marketprice":0
                    }
                ],
                "relation_item":[

                ],
                "productmainimage":"/maincorp/uploadfile/productimage/20200211/164_164/991382886185598976.jpg",
                "productname":"2開F系列總線開關面板(塑料)",
                "productmodelname":"TPN-F/S2",
                "pid":"1038258231194845184"
            },
            {
                "productbrandpid":"1038518770353471488",
                "relation_part":[

                ],
                "productsummary":"> 面板材質爲塑料
> 可作爲燈控門窗開關和場景開關,聯動場景
> 帶狀態反饋和指示燈以及背光
> 指示燈顏色可根據情景而改變
> 按鍵字體可單獨定製(收費)
> 邊框顏色可選黑色、灰色",
                "productbrandname":"AIKS/埃克思",
                "productunit":"塊",
                "children":[
                    {
                        "productsalesprice":"0",
                        "productcoloritem":"2",
                        "productstock":0,
                        "productmainimage":"",
                        "pid":"1038258254280294400",
                        "productlowprice":"0",
                        "productcostprice":"0",
                        "marketprice":0
                    }
                ],
                "relation_item":[

                ],
                "productmainimage":"/maincorp/uploadfile/productimage/20200211/164_164/991387600985948160.jpg",
                "productname":"1開F系列總線開關面板(塑料)",
                "productmodelname":"TPN-F/S1",
                "pid":"1038258268238938112"
            },
            {
                "productbrandpid":"1038518770353471488",
                "relation_part":[

                ],
                "productsummary":"可作爲燈控門窗開關和場景開關
> 帶狀態反饋和指示燈以及背光
> 指示燈顏色可根據情景而改變
> 按鍵字體可單獨定製
> 邊框顏色可選",
                "productbrandname":"AIKS/埃克思",
                "productunit":"塊",
                "children":[
                    {
                        "productsalesprice":"0",
                        "productcoloritem":"2",
                        "productstock":0,
                        "productmainimage":"",
                        "pid":"1038258291861258240",
                        "productlowprice":"0",
                        "productcostprice":"0",
                        "marketprice":0
                    }
                ],
                "relation_item":[

                ],
                "productmainimage":"/maincorp/uploadfile/productimage/20200212/164_164/935468461453115392.jpg",
                "productname":"T系列總線開關面板",
                "productmodelname":"TPN-S4",
                "pid":"1038258305819901952"
            },
            {
                "relation_part":[

                ],
                "productsummary":" CAN-BUS總線接口",
                "productbrandname":"AIKS/埃克思",
                "relation_item":[

                ],
                "productmainimage":"/maincorp/uploadfile/productimage/20200211/164_164/993108537473138688.jpg",
                "pid":"1038258451848790016",
                "productlowprice":0,
                "productcostprice":0,
                "productbrandpid":"1038518770353471488",
                "productsalesprice":0,
                "productunit":"只",
                "children":[

                ],
                "productname":"3路0-10V信號放大器",
                "productmodelname":"TLD-3A1"
            },
            {
                "relation_part":[

                ],
                "productsummary":"> 導軌式3路1-10V,PWM輸出LED驅動器",
                "productbrandname":"AIKS/埃克思",
                "relation_item":[

                ],
                "productmainimage":"/maincorp/uploadfile/productimage/20200211/164_164/993094096182476800.jpg",
                "pid":"1038258476007981056",
                "productlowprice":0,
                "productcostprice":0,
                "productbrandpid":"1038518770353471488",
                "productsalesprice":0,
                "productunit":"只",
                "children":[

                ],
                "productname":"3路0-10V調光驅動器",
                "productmodelname":"TLD-3S8"
            },
            {
                "relation_part":[

                ],
                "productsummary":"> 導軌式單路1-10V(控制燈光色彩65535種顏色切換)
> CAN-BUS總線接口",
                "productbrandname":"AIKS/埃克思",
                "relation_item":[

                ],
                "productmainimage":"/maincorp/uploadfile/productimage/20200211/164_164/993972136747892736.jpg",
                "pid":"1038258499630301184",
                "productlowprice":0,
                "productcostprice":0,
                "productbrandpid":"1038518770353471488",
                "productsalesprice":0,
                "productunit":"只",
                "children":[

                ],
                "productname":"1路0-10V(RGB)調光模塊",
                "productmodelname":"LSS-G1"
            },
            {
                "relation_part":[

                ],
                "productsummary":"> 導軌式3路1-10V(控制燈光亮度)
> CAN-BUS總線接口",
                "productbrandname":"AIKS/埃克思",
                "relation_item":[

                ],
                "productmainimage":"/maincorp/uploadfile/productimage/20200211/164_164/993950120208662528.jpg",
                "pid":"1038258523789492224",
                "productlowprice":0,
                "productcostprice":0,
                "productbrandpid":"1038518770353471488",
                "productsalesprice":0,
                "productunit":"只",
                "children":[

                ],
                "productname":"3路0-10V調光模塊",
                "productmodelname":"LSS-D/V3"
            },
            {
                "relation_part":[

                ],
                "productsummary":">5路LED調光輸出(控制燈光明暗)
>CAN-BUS總線接口",
                "productbrandname":"AIKS/埃克思",
                "relation_item":[

                ],
                "productmainimage":"/maincorp/uploadfile/productimage/20200211/164_164/991975788704071680.jpg",
                "pid":"1038258548485554176",
                "productlowprice":0,
                "productcostprice":0,
                "productbrandpid":"1038518770353471488",
                "productsalesprice":0,
                "productunit":"只",
                "children":[

                ],
                "productname":"5路0-10V調光模塊",
                "productmodelname":"LSS-D/V5"
            },
            {
                "relation_part":[

                ],
                "productsummary":">2路調光輸出,單路1KW(控制燈光明暗)
>CAN-BUS總線接口",
                "productbrandname":"AIKS/埃克思",
                "relation_item":[

                ],
                "productmainimage":"/maincorp/uploadfile/productimage/20200211/164_164/991957882985414656.jpg",
                "pid":"1038258573181616128",
                "productlowprice":0,
                "productcostprice":0,
                "productbrandpid":"1038518770353471488",
                "productsalesprice":0,
                "productunit":"只",
                "children":[

                ],
                "productname":"2路可控硅調光模塊",
                "productmodelname":"LSS-D/S2"
            },
            {
                "relation_part":[

                ],
                "productsummary":">8路輸出,單路16A(控制燈光開、關)
>CAN-BUS總線接口",
                "productbrandname":"AIKS/埃克思",
                "relation_item":[

                ],
                "productmainimage":"/maincorp/uploadfile/productimage/20200211/164_164/994001500365553664.jpg",
                "pid":"1038258597877678080",
                "productlowprice":0,
                "productcostprice":0,
                "productbrandpid":"1038518770353471488",
                "productsalesprice":0,
                "productunit":"只",
                "children":[

                ],
                "productname":"8路功率繼電器模塊",
                "productmodelname":"LSS-P8"
            },
            {
                "relation_part":[

                ],
                "productsummary":">4路輸出,單路30A(控制燈光開、關)
>CAN-BUS總線接口",
                "productbrandname":"AIKS/埃克思",
                "relation_item":[

                ],
                "productmainimage":"/maincorp/uploadfile/productimage/20200211/164_164/993989689205489664.jpg",
                "pid":"1038258623110610944",
                "productlowprice":0,
                "productcostprice":0,
                "productbrandpid":"1038518770353471488",
                "productsalesprice":0,
                "productunit":"只",
                "children":[

                ],
                "productname":"4路功率繼電器模塊",
                "productmodelname":"LSS-P4"
            }
        ]
    },
    "message":null,
    "date":"2020-06-04 11:50:26"
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章