PHP $_POST 和 extract() 除了可能是一个强制转换的 int 外什么也没有显示

PHP $_POST and extract() showing nothing except possibly a casted int

我有一个使用 post

的简单表格
<form id="quote_form" name="n_quote_form" method="post" action="quote">

(此处的操作是一个 url 参数(某种程度上),页面加载脚本使用该参数将用户引导回此页面 in_quote.php

switch ($urlParam) {
        case 'quote':
            $incPage = "in_quote.php";
            break;
        case 'contact':
            $incPage = "in_contact.php";
            break;
        case 'links':
            $incPage = "in_links.php";
            break;
        default:
            $incPage = "in_home.php";
            break;
    }

所以当我用这些数据填写表格时

此数据传递给此脚本:

function submit_quote_form(){

    if (validate_form($_POST)){

依次调用此脚本:(我没有实施验证,直到我可以使用 extract()$_POST 中提取表单数据,因为我不想手动输入键每次表单更改或者我想在其他地方重用我的验证脚本(修改较少)时)

这个脚本正确地转储了 $form_data var(本质上是 $_POST 所以我认为我们可以假设这不是传递参数的问题)但是提取变量 $var 似乎要转换为大小为 11 的 int???

function validate_form($form_data) {
    var_dump($form_data);
    $var = extract( $form_data, EXTR_OVERWRITE, "form_" );
    var_dump($var);
    echo "THIS IS MY POST VAR ==>";
    var_dump($_POST); // included for comparison
}

当我 var_dump $form_data 结果很奇怪 (比较 $form_data$_POST 以确保它不是参数传递问题)

输出:

array(11) { ["n_name"]=> string(3) "bob" ["n_email"]=> string(12) "bob@bobs.com" ["n_email2"]=> string(12) "bob@bobs.com" ["n_day_from"]=> string(1) "8" ["n_month_from"]=> string(8) "December" ["n_year_from"]=> string(4) "2015" ["n_day_to"]=> string(1) "9" ["n_month_to"]=> string(8) "December" ["n_year_to"]=> string(4) "2015" ["n_bike_reqs"]=> string(14) "5 bikes please" ["submit_but"]=> string(13) "get my quotes" } int(11) THIS IS MY POST VAR ==>array(11) { ["n_name"]=> string(3) "bob" ["n_email"]=> string(12) "bob@bobs.com" ["n_email2"]=> string(12) "bob@bobs.com" ["n_day_from"]=> string(1) "8" ["n_month_from"]=> string(8) "December" ["n_year_from"]=> string(4) "2015" ["n_day_to"]=> string(1) "9" ["n_month_to"]=> string(8) "December" ["n_year_to"]=> string(4) "2015" ["n_bike_reqs"]=> string(14) "5 bikes please" ["submit_but"]=> string(13) "get my quotes" }

我确实尝试使用以下方法将提取转换为数组:

$var = (array) extract( $_POST, EXTR_OVERWRITE, "form_" );

但这给了我这个:

array(1) { [0]=> int(11) }

var_dump($var);

所以只将整数转换为长度为 1 的整数数组! 我可以像这里的其他 post 所建议的那样使用单独的键,但我不想特别是当其他 post 已经使用提取方法接受了答案时 - 它对我来说不起作用!

问。为什么 extract() 没有像 I/WE 认为应该的那样工作???

感谢帮助!

extract 会将数组键和值添加到当前符号 table,因此在数组上使用 extract 后,您可以使用数组键作为变量名。它将 return 数组值。

$size = "large";
$var_array = array("color" => "blue",
               "size"  => "medium",
               "shape" => "sphere");
$var=extract($var_array, EXTR_PREFIX_SAME, "wddx");

echo "$color, $size, $shape, $wddx_size\n";
echo $var;

我得到的结果是:

蓝色、大号、球形、中号 3