gpt4 book ai didi

php - if 语句位于数组内?

转载 作者:行者123 更新时间:2023-12-03 19:34:46 25 4
gpt4 key购买 nike

我有一个数组,每个数组都有一个值。

这是一个例子:

$sales_payload = array(
'organization_id' => $organization_id,
'contact_id' => $contact_id,
'status' => 'Open',
'subject' => $_product->post_title." ".str_replace($strToRemove, "", $_POST['billing_myfield12']),
'start_date' => date("Y-m-d"), // set start date on today
'expected_closing_date' => date("Y-m-d",strtotime(date("Y-m-d")."+ 14 days")), // set expected closing date 2 weeks from now
'chance_to_score' => '10%',
'expected_revenue' => 0, //set the expected revenue
'note' => $_POST['order_comments'],

'progress' => array(
'id'=>'salesprogress:200a53bf6d2bbbfe' //fill a valid salesprogress id to set proper sales progress
),

"custom_fields"=> [[
if(strpos($_POST['billing_myfield13'], 'ja') !== false) { [["actief_in_duitsland"=>1]] }
]]
);

我现在正在尝试在我的代码中填充某个数组:

"custom_fields"=>  [["actief_in_duitsland"=>1]] 

这有效。唯一的问题是我希望在特定条件下该值=>1。这个条件是,如果某个POST请求包含某个字符串,则将该值设置为=> 1

我试过这个:

"custom_fields"=> [[ 
if(strpos($_POST['billing_myfield13'], 'ja') !== false) { [["actief_in_duitsland"=>1]] }
]]

所以

if(strpos($_POST['billing_myfield13'], 'ja') !== false) { [["actief_in_duitsland"=>1]] }

如果$_POST['billing_myfield13']包含单词“ja”

然后[["actief_in_duitsland"=>1]]

最佳答案

您可以使用三元条件,这是 if 语句的快捷方式。以下是三元条件如何工作的示例:

常规的 if 可以这样写:

if( "mycondition" == 1 )
{
$boolean = true;
}
else {
$boolean = false;
}

该语句与三元条件的等效写法如下:

$boolean = ( "mycondition" == 1 ) ? true : false;

您可能想使用此快捷方式实例化您的数组,如下所示:

$sales_payload = [
// ...
'custom_fields' => ( strpos($_POST['billing_myfield13'], 'ja') !== false ) ? [['actief_duitsland' => 1]] : [['actief_duitsland' => '???']],
// ...
];

警告

您还应该为此语句定义一个 else 值。

关于php - if 语句位于数组内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40761332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com