1

I am getting some values (domain names) from a _POST which I have to insert into an "Array in an Array". The array is called $postValues["domainrenewals"] and the I need to create another array inside this one in the format:

domainname => 1 (where 1 is the number of years).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"]

Any help, much appreciated.

2 Answers 2

5

In each pass of the foreach loop you're redefining $postValues["domainrenewals"] so of course only the last one is saved... Try doing this:

$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;
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry to bother again, say I need to add another pair to $postValues["domainrenewals"] ? For instance it appears I need to extend $postValues["domainrenewals"][$value] = "1"; to something like $postValues["domainrenewals"][$value] = "1", [renewalpriceoverride] = 285.00; tried this but adding another to the array is a problem.
Updated the answer... please mark it as the answer (checkmark on the left) if it helped you
0

Try This:

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.