简体   繁体   English

插入键值-数组中的数组

[英]Insert key value - array in array

I am getting some values (domain names) from a _POST which I have to insert into an "Array in an Array". 我从_POST获取一些值(域名),我必须将它们插入“数组中的数组”中。 The array is called $postValues["domainrenewals"] and the I need to create another array inside this one in the format: 该数组称为$postValues["domainrenewals"] ,我需要在该数组中以以下格式创建另一个数组:

domainname => 1 (where 1 is the number of years).n domainname => 1(其中1是年数)。n

My code: 我的代码:

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

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

var_dump ($postData);

The var_dump shows that only the last $key -> $value pair is being inserted into $postValues["domainrenewals"] var_dump显示仅将最后一个$postValues["domainrenewals"] > $ value对插入到$postValues["domainrenewals"]

Any help, much appreciated. 任何帮助,不胜感激。

In each pass of the foreach loop you're redefining $postValues["domainrenewals"] so of course only the last one is saved... Try doing this: foreach循环的每一遍中,您都在重新定义$postValues["domainrenewals"]因此当然只保存了最后一个...请尝试执行以下操作:

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

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

If you need to add another value to the array I'm assuming it's information of the domain, so you would do something like: 如果您需要向数组添加另一个值,我假设它是域的信息,那么您可以执行以下操作:

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

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

Try This: 尝试这个:

$postValues = array();
$arr=array();

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM