Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:将数组发布到函数并创建多维数组_Php_Multidimensional Array_Foreach - Fatal编程技术网

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

PHP:将数组发布到函数并创建多维数组,php,multidimensional-array,foreach,Php,Multidimensional Array,Foreach,我需要创建与此类似的数组: $need = Array( "smsSend" => Array( "account" => Array( "user" => 123, "password" => "xxxxxx", "profile" => 123456 ) ), "smsConfig" => Array ( "region" => Array (

我需要创建与此类似的数组:

$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"
    )
)
);
从$U 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);
参数的值为array
$post
,问题是当我这样打印结果
$info
时,选项为空:

Array
(
    [smsSend] => 
    [smsConfig] => 
    [templateConfig] => 
    [fieldsConfig] => 
    [serverConfig] => 
    [fields] => Array
        (
            [sms] => 
            [email] => 
        )
)

我写了这段代码。我认为这是有效的。但问题是存在的。当我使用explode时,它也会被分割。尝试另一种方法,比如strstr。用递归函数试试看

我不能理解你的问题。您的输入数组是什么?您想要什么作为输出数组?输入数组是$post数组第二个数组,输出数组指针是$need第一个数组,问题是当迭代$post数组并通过函数重新调用此数组时([smsSend]=>[smsConfig]=>[templateConfig]=>[fieldsConfig]=>[serverConfig]=>[fields]=>数组([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]);               
            } 
        }
    }          
}