猿问

有条件地将数组添加为另一个数组中的变量

这就是数组的最终输出应该是这样的


$dataToReset = array (

    'email_address' => $subEmail,

    'status' => 'subscribed',

    'interests' => 

        array (

        '1111111' => true,

        '2222222' => true,

        '3333333' => true,

        '4444444' => true,

        '5555555' => true,

    )

);

我想更换以下部分


'interests' => 

    array (

    '1111111' => true,

    '2222222' => true,

    '3333333' => true,

    '4444444' => true,

    '5555555' => true,

)

$interestsAdd有了这样的变量


$dataToReset = array (

    'email_address' => $subEmail,

    'status' => 'subscribed',

    $interestsAdd

);

我得到的值和我尝试过的值如下,但没有成功!


if ($form_data['lbu_multilistsvalue'] !== ''){

    

    $groupsSelected = $form_data['lbu_multilistsvalue'];

    $selectedGroups = array_fill_keys(explode(",", $groupsSelected), true);


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


} else {


    $interestsAdd = '';


}


慕盖茨4494581
浏览 97回答 2
2回答

LEATH

您有多种选择,可以在数组中定义键:$interestsAdd = [1,2,3];$dataToReset = array (    'email_address' => 'x',    'status' => 'subscribed',    'interests' => $interestsAdd);或者之后添加:$interestsAdd = [1,2,3];$dataToReset = array (    'email_address' => 'x',    'status' => 'subscribed',);$dataToReset['interests'] = $interestsAdd;或者与您当前的结构合并它们:$interestsAdd = ['interests' => [1,2,3]];$dataToReset = array_merge($dataToReset, $interestsAdd);

肥皂起泡泡

尝试像这样设置你的第一个数组:$dataToReset = [    'email_address' => $subEmail,    'status' => 'subscribed',    'interests' => [],];构建 $interestsAdd 数组后,将其添加到第一个数组:$dataToReset['interests'] = $interestsAdd;
随时随地看视频慕课网APP
我要回答