有条件地将数组添加为另一个数组中的变量
Add array conditionally as a variable in an another array conditionally
数组的最终输出应该是这样的
$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 = '';
}
您有多种选择,要么在数组中定义键:
$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' => [],
];
构建 $interests 添加数组后,将其添加到第一个数组:
$dataToReset['interests'] = $interestsAdd;
数组的最终输出应该是这样的
$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 = '';
}
您有多种选择,要么在数组中定义键:
$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' => [],
];
构建 $interests 添加数组后,将其添加到第一个数组:
$dataToReset['interests'] = $interestsAdd;