编辑代码

<?php 
$jsonData = '{
    "wlan": {
        "2_4G": [
            {
                "ssid": "2.4G-1",
                "ssid_password": "12345678",
                "ssid_enable": 1,
                "ssid_broadcast": 1,
                "encryption": 2
            },
            {
                "ssid": "2.4G-2",
                "ssid_password": "12345678",
                "ssid_enable": 1,
                "ssid_broadcast": 1,
                "encryption": 2
            },
            {
                "ssid": "2.4G-3",
                "ssid_password": "12345678",
                "ssid_enable": 1,
                "ssid_broadcast": 1,
                "encryption": 2
            },
            {
                "ssid": "2.4G-4",
                "ssid_password": "12345678",
                "ssid_enable": 1,
                "ssid_broadcast": 1,
                "encryption": 2
            }
        ],
        "5G":[{},{},{},{}]
    },
    "wan": [
        {
            "enable_vlan": 1,
            "vlanId": 100,
            "priority": 2,
            "service_type": 1,
            "connection_type": 2,
            "mtu": 1500,
            "enable_igmp_mld_proxy": 1,
            "multicast_vlan_id": 1001,
            "ip_protocol": 1,
            "enable_napt": 1,
            "ipv4": {
                "dhcp_enable": 1,
                "request_dns": 1,
                "dns": [
                    "1.1.1.1",
                    "8.8.8.8"
                ]
            }
        },
        {
            "enable_vlan": 1,
            "vlanId": 200,
            "priority": 3,
            "service_type": 2,
            "connection_type": 2,
            "mtu": 1500,
            "enable_igmp_mld_proxy": 1,
            "multicast_vlan_id": 1002,
            "ip_protocol": 1,
            "enable_napt": 1,
            "ppp": {
                "username": "csb",
                "password": "csb",
                "ac_name": "",
                "service_name": ""
            }
        }
    ]
}';

// $decode_data = json_encode($jsonData, true);
// print_r($decode_data);
// echo $decode_data."\n";

$obj = json_decode($jsonData, true);
// print_r($obj) ;

// if (json_last_error() === JSON_ERROR_NONE) {
//     $ssidNames = array_map(function($item) {
//         return $item['ssid'];
//     }, $arrayData['wlan']['2_4G']);
    
//     $combinedSsids = implode(';', $ssidNames);
//     echo "Combined SSID names: " . $combinedSsids;
// } else {
//     echo "Error parsing JSON: " . json_last_error_msg();
// }

    $wanConnType = "";
    $wanServType = "";

    $arrtest = ['', '', '', '', '', '', '', ''];
    print_r($arrtest);
    $strtest = implode(";", $arrtest);
    print_r($strtest);
    echo "\n";

    $arrtest2 = array();
    // $arrtest2 = implode(";", $strtest);
    // print_r($strtest2);

    if(json_last_error() === JSON_ERROR_NONE) {
        foreach($obj as $key1 => $value1) {
            echo "foreach1: key1= ".$key1."\n";
            if($key1 === "wlan"){
                $arrSsidName = ['', '', '', '', '', '', '', ''];
                $arrSsidPwd = ['', '', '', '', '', '', '', ''];
                $arrSsidEnable = ['', '', '', '', '', '', '', ''];
                // $arrSsidBroadcast = ['', '', '', '', '', '', '', ''];
                // $arrSsidEncryption = ['', '', '', '', '', '', '', ''];
                // $arrSsidName = [];
                // $arrSsidPwd = [];
                // $arrSsidEnable = [];
                $arrSsidBroadcast = [];
                $arrSsidEncryption = [];

                foreach($value1 as $key2 => $value2) {
                    echo "\tforeach2: key2= ".$key2."\n";
                    if($key2 === '2_4G') {
                        for($i=0;$i<4;$i++){
                            foreach($value2[$i] as $key3 => $value3) {
                                echo "\t\tkey3= ".$key3.", value3= ".$value3."\n";
                                switch($key3)
                                {
                                    case "ssid":
                                        $arrSsidName[$i] = $value3;
                                        break;
                                    case "ssid_password":
                                        $arrSsidPwd[$i] = $value3;
                                        // array_push($arrSsidPwd,$value3);
                                        break;
                                    case "ssid_enable":
                                        $arrSsidEnable[$i] = strval($value3);
                                        break;
                                    case "ssid_broadcast":
                                        array_push($arrSsidBroadcast,$value3);
                                        break;
                                    case "encryption":
                                        array_push($arrSsidEncryption,$value3);
                                        break;
                                    default:
                                }
                            }
                        }
                    }
                    if($key2 === '5G') {
                        for($i=0;$i<4;$i++){
                            foreach($value2[$i] as $key3 => $value3) {
                                echo "\t\tkey3= ".$key3.", value3= ".$value3."\n";
                                switch($key3)
                                {
                                    case "ssid":
                                        $arrSsidName[$i+4] = $value3;
                                        break;
                                    case "ssid_password":
                                        $arrSsidPwd[$i+4] = $value3;
                                        break;
                                    case "ssid_enable":
                                        $arrSsidEnable[$i] = strval($value3);
                                        break;
                                    case "ssid_broadcast":
                                        array_push($arrSsidBroadcast,$value3);
                                        break;
                                    case "encryption":
                                        array_push($arrSsidEncryption,$value3);
                                        break;
                                    default:
                                }
                            }
                        }
                    }                
                    print_r($arrSsidName);
                    $ssidname = implode(";", $arrSsidName);
                    echo "ssid= ".$ssidname."\n";
                    $ssidpwd = implode(";", $arrSsidPwd);
                    echo "ssid= ".$ssidpwd."\n";
                    $ssidenable = implode(";", $arrSsidEnable);
                    echo "ssid= ".$ssidenable."\n";
                    // $ssidbroadcast = implode(";", $arrSsidBroadcast);
                    // echo "ssid= ".$ssidbroadcast."\n";
                    // $ssidencryt = implode(";", $arrSsidEncryption);
                    // echo "ssid= ".$ssidencryt."\n";
                    // echo isset($arrSsidEncryption)."\n";
                    // echo empty($arrSsidEncryption)."\n";
                    // var_dump(empty($ssidencryt));

                }
            } else if($key1 === "wan1") {
                $arrLength = count($value1);
                //var_dump($arrLength);
                $arrTotalWan=[];
                //print_r($value1);
                for($i=0;$i<$arrLength;$i++) {
                    $arrWan = [];

                    foreach($value1[$i] as $key2 => $value2) {
                        echo "foreach2: key2= ".$key2.", value2= ".$value2."\n";
                        //print_r($value2."\n");
                        switch($key2)
                        {
                            case "service_type":
                                if($value2 === 0) {
                                    $tempStr = '"ConnType":"BRIDGE"';
                                    array_push($arrWan, $tempStr);
                                } else if($value2 === 1) {
                                    $tempStr = '"ConnType":"ROUTE"';
                                    array_push($arrWan, $tempStr);
                                    $tempStr = '"WANCType":"IP"';
                                    array_push($arrWan, $tempStr);
                                } else if($value2 === 2) {
                                    $tempStr = '"ConnType":"ROUTE"';
                                    array_push($arrWan, $tempStr);
                                    $tempStr = '"WANCType":"PPPOE"';
                                    array_push($arrWan, $tempStr);
                                }
                                break;
                            case "enable_vlan":
                                if($value2 === 1) {
                                    $tempStr = '"VLANMode":"tag"';
                                } else {
                                    $tempStr = '"VLANMode":"untag"';
                                }
                                array_push($arrWan, $tempStr);
                                break;
                            case "vlanId":
                                $tempStr = '"VLANID":'.$value2;
                                array_push($arrWan, $tempStr);
                                break;
                            case "priority":
                                $tempStr = '"Priority":'.$value2;
                                array_push($arrWan, $tempStr);
                                break;
                            case "mtu":
                                $tempStr = '"MTU":'.$value2;
                                array_push($arrWan, $tempStr);
                                break;
                            case "enable_igmp_mld_proxy":
                                $tempStr = '"IgmpMldEnable":'.$value2;
                                array_push($arrWan, $tempStr);
                                break;
                            case "multicast_vlan_id":
                                $tempStr = '"MulticastVLANID":'.$value2;
                                array_push($arrWan, $tempStr);
                                break;
                            case "ip_protocol":
                                $tempStr = '"IPprotocol":'.$value2;
                                array_push($arrWan, $tempStr);
                                break;
                            case "enable_napt":
                                $tempStr = '"IsNAT":'.$value2;
                                array_push($arrWan, $tempStr);
                                break;
                            case "ppp":
                                foreach($value2 as $key3 => $value3) {
                                    echo "foreach3: key3= ".$key3.", value3= ".$value3."\n";
                                    switch($key3)
                                    {
                                        case "username":
                                            $tempStr = '"PPPoEUserName":"'.$value3.'"';
                                            array_push($arrWan, $tempStr);
                                            break;
                                        case "password":
                                            $tempStr = '"PPPoEPassword":"'.$value3.'"';
                                            array_push($arrWan, $tempStr);
                                            break;
                                        case "ac_name":
                                            $tempStr = '"PPPoEACName":"'.$value3.'"';
                                            array_push($arrWan, $tempStr);
                                            break;
                                        case "service_name":
                                            $tempStr = '"PPPoEServiceName":"'.$value3.'"';
                                            array_push($arrWan, $tempStr);
                                            break;
                                        default:
                                    }
                                }
                                break;
                            case "ipv4":
                                foreach($value2 as $key3 =>$value3) {
                                    switch($key3)
                                    {
                                        case "dns":
                                            if($value3[0]) {
                                                $tempStr = '"DNS1":"'.$value3[0].'"';
                                                array_push($arrWan, $tempStr);
                                            }
                                            if($value3[1]) {
                                                $tempStr = '"DNS1":"'.$value3[1].'"';
                                                array_push($arrWan, $tempStr);
                                            }
                                            break;
                                        default:
                                    }
                                }
                            default:
                        }
                    }
                    if(!empty($arrWan)) {
                        print_r($arrWan);
                        //echo "\n";
                        $strWan='"WAN'.($i+1).'":{'.implode(",", $arrWan).'}';
                        echo "strWan= ".$strWan."\n";
                        array_push($arrTotalWan, $strWan);
                    }
                }
                print_r($arrTotalWan);
                $strTotolWan="{".implode(",", $arrTotalWan)."}";
                echo "strTotolWan= ".$strTotolWan."\n";
            }
        }
        var_dump(date('Y-m-d_H-i-s'));
    }