PHP:将数组发布到函数并创建多维数组



我需要创建一个类似的数组:

$need = Array(
"smsSend" => Array(
    "account" => Array(
        "user" => 123,
        "password" => "xxxxxx",
        "profile" => 123456
    )
),
"smsConfig" => Array
    (
    "region" => Array
        (
        "locale" => "es_ES",
        "timezone" => "America/Bogota"
    ),
    "send" => Array
        (
        "from" => "9:00:00",
        "to" => "21:00:00"
    ),
    "sms" => Array
        (
        "channel" => "SMS",
        "from" => "LINIO",
    )
),
"templateConfig" => Array
    (
    "template" => Array
        (
        "postpago" => 1111,
        "prepago" => 0010,
        "notificar" => 1112
    )
),
"fieldsConfig" => Array
    (
    "fields" => Array
        (
        "nombre" => "firstname",
        "carrier" => "nome_transportadora",
        "track" => "track",
        "cantidad" => "total_depois_de_impostos"
    )
),
"serverConfig" => Array
    (
    "test" => "http://miportal",
    "prod" => "",
    "mode" => "test",
    "adapter" => "curl",
    "type" => "post",
    "telephone" => "12345"
),
"fields" => Array
    (
    "sms" => Array
        (
        "address" => "mobile_phone"
    ),
    "email" => Array
        (
        "address" => "email_cliente"
    )
)
);

根据此安排从$_POST接收:

$post = Array(
"smsSend" => Array(
    "account" => Array(
        "user:123",
        "password:xxxxxx",
        "profile:123456"
    )
),
"smsConfig" => Array
    (
    "region" => Array
        (
        "locale:es_ES",
        "timezone:America/Bogota"
    ),
    "send" => Array
        (
        "from:9:00:00",
        "to:21:00:00"
    ),
    "sms" => Array
        (
        "channel:SMS",
        "from:LINIO",
    )
),
"templateConfig" => Array
    (
    "template" => Array
        (
        "postpago:1111",
        "prepago:0010",
        "notificar:1112"
    )
),
"fieldsConfig" => Array
    (
    "fields" => Array
        (
        "nombre:firstname",
        "carrier:nome_transportadora",
        "track:track",
        "cantidad:total_depois_de_impostos"
    )
),
"serverConfig" => Array
    (
    "test:http://miportal",
    "prod:",
    "mode:test",
    "adapter:curl",
    "type:post",
    "telephone:12345"
),
"fields" => Array
    (
    "sms" => Array
        (
        "address:mobile_phone"
    ),
    "email" => Array
        (
        "address:email_cliente"
    )
)
);

问题是组装父项的循环,我使用树函数创建级别:

function createLevel1($array_data) {
        $array_push = array();
        foreach ($array_data as $key_l1 => $elem_l1) {
            array_push($array_push[$key_l1], "");
            createLevel2($elem_l1, $array_push, $key_l1);
            // print_r($resl1);
        }
        return $array_push;
    }
    function createLevel2($elemento_array, $push_array, $parent_key) {
        foreach ($elemento_array as $key_l2 => $elem_l2) {
            if (is_array($elem_l2)) {
                $push_array[$parent_key][$key_l2] = "";
                createLevel3($elem_l2, $push_array, $parent_key, $key_l2);                    
            } else {
                $items = explode("::", $elem_l2);
                $push_array[$parent_key][$items[0]] = $items[1];               
            }
        }
        return $push_array;
    }
    function createLevel3($elemento_array, $push_array, $parent_key, $parentl2_key) {
        $push_array[$parent_key][$parentl2_key] = "";
        foreach ($elemento_array as $key_l3 => $elem_l3) {
            if (is_array($elem_l3)) {
                createLevel3($elem_l3, $push_array, $parent12_key, $key_l3);
            } else {
                $items = explode("::", $elem_l3);
                $push_array[$parent_key][$parentl2_key][$items[0]] = $items[1];
            }
        }
        return $push_array;
    }
    $parameter = $_POST['postData'];
    @$info = createLevel1($parameter);
    print_r($parameter);

参数的值是数组$post,问题是当我像这样打印结果$info时,选项为空。:

Array
(
    [smsSend] => 
    [smsConfig] => 
    [templateConfig] => 
    [fieldsConfig] => 
    [serverConfig] => 
    [fields] => Array
        (
            [sms] => 
            [email] => 
        )
)
$need = array();
foreach ($post as $key => $value) {
    if (is_array($value)) {
        $need[$key] = $value;
        foreach ($value as $key2 => $value2) {
            if (is_array($value2)) {
                foreach ($value2 as $key3 => $value3) {
                    if (is_array($value3)) {
                    } elseif (strpos($value3, ':') !== FALSE) {
                        $tmp3 = explode(':', $value3);
                        $need[$key][$key2][$tmp3[0]] = $tmp3[1];
                        unset($need[$key][$key2][$key3]);               
                    } 
                }    
            } elseif (strpos($value2, ':') !== FALSE) {
                $tmp2 = explode(':', $value2);
                $need[$key][$tmp2[0]] = $tmp2[1];
                unset($need[$key][$key2]);               
            } 
        }
    }          
}

我已经写了这个代码。我认为这是有效的。但问题是存在的。当我使用爆炸时,它会分开http://miportal.com而且尝试另一种方法,如strstr。试试递归函数。

相关内容

  • 没有找到相关文章

最新更新