插入键值 - 数组中的数组



我正在从_POST中获取一些值(域名(,我必须将其插入到"数组中的数组"中。 该数组称为 $postValues["domainrenewals"] 我需要在此数组中创建另一个数组,格式如下:

域名 => 1(其中 1 是年数(.n

我的代码:

foreach ($_POST['renewthesedomains'] as $key => $value) {
   $postValues["domainrenewals"] = array($value => "1");
}
var_dump ($postData);

var_dump显示只有最后的 $key -> $value 对入到$postValues["domainrenewals"]

任何帮助,非常感谢。

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

$postValues["domainrenewals"] = array();
foreach ($_POST['renewthesedomains'] as $key => $value) {
    $postValues["domainrenewals"][$value]  = "1";
}

如果您需要向数组添加另一个值,我假设它是域的信息,因此您可以执行以下操作:

$postValues["domainrenewals"][$value]['your_first_value'] = "1";
// Then for your other value
$postValues["domainrenewals"][$value]['renewalpriceoverride'] = 285.00;

试试这个:

$postValues = array();
$arr=array();
foreach ($_POST['renewthesedomains'] as $value) {
    $arr["domainrenewals"]=$value;
    $arr["no_of_years"]=1;
    $postValues[]  = $arr;
    $arr=array();
}

最新更新