1

This is how the final output of the array should look like

$dataToReset = array (
    'email_address' => $subEmail,
    'status' => 'subscribed',
    'interests' => 
        array (
        '1111111' => true,
        '2222222' => true,
        '3333333' => true,
        '4444444' => true,
        '5555555' => true,
    )
);

I want to replace following part

'interests' => 
    array (
    '1111111' => true,
    '2222222' => true,
    '3333333' => true,
    '4444444' => true,
    '5555555' => true,
)

With a variable $interestsAdd like this

$dataToReset = array (
    'email_address' => $subEmail,
    'status' => 'subscribed',
    $interestsAdd
);

The values that i get and what i have tried is like following, but no success!

if ($form_data['lbu_multilistsvalue'] !== ''){
    
    $groupsSelected = $form_data['lbu_multilistsvalue'];
    $selectedGroups = array_fill_keys(explode(",", $groupsSelected), true);

    $interestsAdd = ['interests' => $selectedGroups];

} else {

    $interestsAdd = '';

}

4
  • What do you get? Commented Jul 29, 2020 at 19:35
  • @AbraCadaver I should get the first part of code in my question Commented Jul 29, 2020 at 19:37
  • Not what SHOULD what DO you get. Commented Jul 29, 2020 at 19:37
  • @AbraCadaver I get an Array of key=>value inside an Array called 'interests'. if this what you mean? Commented Jul 29, 2020 at 19:39

2 Answers 2

1

You have several options, either define the key in the array:

$interestsAdd = [1,2,3];

$dataToReset = array (
    'email_address' => 'x',
    'status' => 'subscribed',
    'interests' => $interestsAdd
);

Or add it afterwards:

$interestsAdd = [1,2,3];

$dataToReset = array (
    'email_address' => 'x',
    'status' => 'subscribed',
);
$dataToReset['interests'] = $interestsAdd;

Or with your current structure, merge them:

$interestsAdd = ['interests' => [1,2,3]];

$dataToReset = array_merge($dataToReset, $interestsAdd);
Sign up to request clarification or add additional context in comments.

1 Comment

The second part works for me!! Thanks dear you have saved me couple of hours!
0

Try setting up your first array like this:

$dataToReset = [
    'email_address' => $subEmail,
    'status' => 'subscribed',
    'interests' => [],
];

Once you have your $interestsAdd array built you add it to the first array:

$dataToReset['interests'] = $interestsAdd;

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.