插入键值 - 数组中的数组

Lin*_*c82 1 php arrays foreach

我从_POST获取了一些值(域名),我必须插入到"数组中的数组"中.调用该数组$postValues["domainrenewals"],我需要在格式中创建另一个数组:

domainname => 1(其中1是年数).n

我的代码:

foreach ($_POST['renewthesedomains'] as $key => $value) {

   $postValues["domainrenewals"] = array($value => "1");
}

var_dump ($postData);
Run Code Online (Sandbox Code Playgroud)

var_dump> $值对被插入-只有最后$键显示$postValues["domainrenewals"]

任何帮助,非常感谢.

jak*_*389 5

foreach循环的每次传递中,您都要重新定义,$postValues["domainrenewals"]因此当然只保存最后一个...尝试这样做:

$postValues["domainrenewals"] = array();

foreach ($_POST['renewthesedomains'] as $key => $value) {
    $postValues["domainrenewals"][$value]  = "1";
}
Run Code Online (Sandbox Code Playgroud)

如果你需要为数组添加另一个值,我假设它是域的信息,所以你会做类似的事情:

$postValues["domainrenewals"][$value]['your_first_value'] = "1";

// Then for your other value
$postValues["domainrenewals"][$value]['renewalpriceoverride'] = 285.00;
Run Code Online (Sandbox Code Playgroud)