如何使用php数组的关键数组

时间:2017-03-27 12:46:11

标签: php arrays

我想制作一系列各种输入。例如,我的密钥是提交按钮,我希望我的数组键是我的提交按钮需要的内容,我的按钮也连接到文本字段。

我的最小代码:

<?php

function element($submit){

    if ($submit == "submit"){ 
        $element = "<input type = $submit value = $submit /><INPUT type = $submit name = $submit value = $submit size=40 />";

    }
    $content = $element ;

    return $content;


} // function element


function main($submit){

//print_r ($defaults);

    foreach ($submit as $k=>$submit){
        @$content .=element ($submit[$k]) . "<br />\n";

    }

    return "<form action = {$_SERVER['PHP_SELF']} method = POST>\n$content</form>";

}



$arr = array(
    "submit" => array("submit", "OK","text", "question", ""),
);

$content .= main ($arr["submit"]);

print $content;

所以问题是我不知道如何将我的键数组值放入HTML中。也许我做错了?这样做是不好的做法?

2 个答案:

答案 0 :(得分:1)

您可以使用PHP中的.(点)运算符将变量值嵌入到字符串中(这通常称为&#34;字符串连接&#34;)。对于你的代码,我会这样做:

$element = "<input type='" . $btnname . "' value='" . $btnvalue . "'/><input type='" . $fieldtype . "' name=" . $fieldname . " value='" . $fieldvalue . "' size='40' />";

或使用数组(必须先定义):

$element = "<input type='" . $sub[0] . "' value='" . $sub[1] . "' /><input type='" . $sub[2] . "' name=" . $sub[3] . " value='" . $sub[4] . "' size='40' />";

如果你有一个(多维)数组(键也是数组),比如说

$arr = array("subkey" => array($firstVar, $secondVar, [...]));

然后你必须像这样使用字符串连接:

$element = "<input type='" . $arr["subkey"][0] . "' value='" . $arr["subkey"][1] . "' /><input type='" . $arr["subkey"][2] . "' name=" . $arr["subkey"][3] . " value='" . $arr["subkey"][4] . "' size='40' />";

这也适用于更多数组子键(更多维度)。

您也可以在数组中使用字符串索引,例如$_SERVER$_GET$_POST中的字符串索引,方法是将整数索引替换为字符串。

但是您不必将提交按钮连接到文本字段,它可以单独存在。

答案 1 :(得分:1)

这是输出我认为你想要的输入。代码中有关于我改变的内容和一些想法的评论。

<?php

//Change param to $name of the index and $data that you will usefor readability
function element($name, $data){
    //Checking that we are on the right element
    $element = "";
    if ($name == "submit"){ 
        //It is confusing that you have 2 inputs here.
        //Rather than hard coding 0, 1, 2, 3, 4 there could be a name
        //in an associative array. This is not very flexible
        //This is string interpolation I like it more hen concatenation for
        //Simple put this variable here
        $element = "<input type='{$data[0]}' value='{$data[1]}' /><input type='{$data[2]}' name='{$data[3]}' value='{$data[4]}' size=40 />";
    }

    //No need to do $content = $element then just return
    //Not sure what should happen if the name does not exist...
    //Right now it is an empty string
    return $element;
}


function main($inputs){

    //print_r ($defaults);

    //Create this before just using it removes a warning.
    $content = '';

    //Change to $v just for clarity not sure what it will do to actual $submit.
    //Loops the whole array not just that instances of submit.
    foreach ($inputs as $k=>$v){
        $content .= element ($k, $v) . "<br />\n";
    }

    return "<form action = {$_SERVER['PHP_SELF']} method = POST>\n$content</form>";

}

//Changed to inputs assuming this would be more then one
$inputs = array(
    "submit" => array("submit", "OK","text", "question", ""),
);

//Lets call this form you are overusing the content and it is hard to follow
$form = main($inputs);

print $form;