douzhongqiu5032 2016-01-08 01:50
浏览 15

在php中创建$ _POST数组中的变量

I am having a $_POST array look like this:

Array
(
    [veryfy_doc_type] => Array
            (
                [0] => 1
                [1] => 2
            )

    [id_number] => Array
            (
                [0] => 3242424
                [1] => 4456889
            )

    [document_issuer] => Array
            (
                    [0] => 1
                    [1] => 3
            )

    [verify_doc_expiry_date] => Array
            (
                    [0] => 2016-01-26
                    [1] => 2015-02-20
            )

    [doc_id] => Array
            (
                    [0] => 15
                    [1] => 16
            )

    [user_id] => Array
            (
                    [0] => 14
                    [1] => 14
            )
)

Using this array I need to get each values into php variables.

I tried it something like this, but it doesn't work for me.

foreach($_POST AS $k => $v) { 
    //print_r($v); 
    list($doc_type, $id_number, $issuer, $expiry_date, $doc_id, $user_id) = $v;
}

echo "Type = $doc_type";

Can anybody tell me how to figure this out. Thank you.

  • 写回答

4条回答 默认 最新

  • douxun2018 2016-01-08 01:52
    关注

    So you want to reference each of the sub-array values while looping the main array... maybe something like this?

    // Loop one of the sub arrays - you need to know how many times to loop!
    foreach ($_POST['veryfy_doc_type'] as $key => $value) {
        // Filter the main array and use the $key (0 or 1) for the callback
        $rowValues = array_map(function($row) use ($key) {
            // Return the sub-array value using the $key (0 or 1) for this level
            return $row[$key];
        }, $_POST);
    
        print_r($rowValues);
    }
    

    Example: https://eval.in/498895

    This would get you structured arrays for each set of data.

    From here I'd suggest you leave the arrays as they are rather than exporting to variables, but if you wanted to you you could use the list() as in your example.

    评论

报告相同问题?