Php 插入到另一个数组中的条件数组输出
我有以下代码创建一个电子邮件活动,并将其发送到mailchimp中的特定部分Php 插入到另一个数组中的条件数组输出,php,arrays,json,conditional-statements,Php,Arrays,Json,Conditional Statements,我有以下代码创建一个电子邮件活动,并将其发送到mailchimp中的特定部分 $data = array("recipients" => array( "list_id" => "$listId", "segment_opts" => array( 'match' => 'any', // or 'all' or 'none'
$data = array("recipients" => array(
"list_id" => "$listId",
"segment_opts" => array(
'match' => 'any', // or 'all' or 'none'
'conditions' => array (
array(
'condition_type' => 'Interests', // note capital I
'field' => 'interests-0a6fa2c02e', // ID of interest category
'op' => 'interestcontains', // or interestcontainsall, interestcontainsnone
'value' => array (
'11111111', // ID of interest in that category
'22222222', // ID of another interest in that category
'33333333',
'44444444',
'55555555'
)
)
)
)
),
"type" => "regular",
"status" => "paused",
//"schedule_time" => $form_data['post_date'],
"schedule_time" => "2020-09-15T15:00:00+00:00",
"timewarp" => false,
"batch_delivery" => array(
"batch_delay" => 0,
"batch_count" => 0
),
"settings" => array(
"subject_line" => $form_data['lbu_subject'],
"title" => $form_data['post_title'],
"from_name" => $form_data['lbu_from'],
"reply_to" => get_option('lubuvna-company-email'),
"template_id" => 999999,
)
);
我可以从下面的表格中获得动态值,它工作得很好:
if ( $form_data['lbu_to'] == 'multilists' && $form_data['lbu_multilistsvalue'] !== '' ){
// get the comma separated values from 'lbu_multilistsvalue' input.
$commaValues = $form_data['lbu_multilistsvalue'];
$lubuvnaCommas = explode(',', $commaValues);
$segment_opts = array(
"segment_opts" => array(
'match' => 'any', // or 'all' or 'none'
'conditions' => array (
array(
'condition_type' => 'Interests', // note capital I
'field' => 'interests-0a6fa2c02e', // ID of interest category
'op' => 'interestcontains', // or interestcontainsall, interestcontainsnone
/*'value' => array (
'11111111', // ID of interest in that category
'22222222', // ID of another interest in that category
'33333333',
'44444444',
'55555555'
)*/
'value' => $lubuvnaCommas // array from values
)
)
)
);
} else {
$segment_opts = '';
}
最终结构应如下所示,但这不起作用:
$data = array("recipients" => array(
"list_id" => "$listId",
$segment_opts,
),
"type" => "regular",
"status" => "paused",
//"schedule_time" => $form_data['post_date'],
"schedule_time" => "2020-09-15T15:00:00+00:00",
"timewarp" => false,
"batch_delivery" => array(
"batch_delay" => 0,
"batch_count" => 0
),
"settings" => array(
"subject_line" => $form_data['lbu_subject'],
"title" => $form_data['post_title'],
"from_name" => $form_data['lbu_from'],
"reply_to" => get_option('lubuvna-company-email'),
"template_id" => 99999,
)
);
我需要检查“lbu_multilistsvalue”是否为空,然后避免在最终结构中输出$segment_opts变量。我总是被困在这样的数组中,需要一些有条件的数据。使用array\u merge()
组合两个数组
$data = array("recipients" => array_merge(array("list_id" => "$listId"), $segment_opts),
"type" => "regular",
"status" => "paused",
//"schedule_time" => $form_data['post_date'],
"schedule_time" => "2020-09-15T15:00:00+00:00",
"timewarp" => false,
"batch_delivery" => array(
"batch_delay" => 0,
"batch_count" => 0
),
"settings" => array(
"subject_line" => $form_data['lbu_subject'],
"title" => $form_data['post_title'],
"from_name" => $form_data['lbu_from'],
"reply_to" => get_option('lubuvna-company-email'),
"template_id" => 99999,
)
);
若要正确执行此操作,当条件为非真时,应将空数组分配给$segment\u opts
,而不是空字符串
} else {
$segment_opts = array();
}