php 中的 DRY 数组
DRY array in php
你好,我正在尝试将 DRY 概念应用到我的程序中,该程序现在有 3000 多行代码,只是因为我一直在重复自己。
我有这个数组:
// prepare the sales payload
$sales_payload = array(
'organization_id' => $getOrg['data'][0]['id'],
'contact_id' => $contact_id,
'status' => 'Open',
'responsible_employee_id' => 'employee:50c76c15262b12d36d44e34a3f0f8c3d',
'source' => array(
'id' => $source['data'][0]['id'],
'name' => $source['data'][0]['name'],
),
'subject' => " ".str_replace($strToRemove, "", $_POST['billing_myfield12'])." - "."Online marketing Duitsland",
'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" => [["voorstel_diensten"=>implode("-",$online_marketing_check)]],
);
有没有一种方法可以只动态更改某些字段,而不是复制整个内容并手动更改它。
您可以声明一个函数来克隆数组并更改它
function arrayClone($args){
$sales_payload = array(
'organization_id' => $getOrg['data'][0]['id'],
'contact_id' => $contact_id,
'status' => 'Open',
'responsible_employee_id' => 'employee:50c76c15262b12d36d44e34a3f0f8c3d',
'source' => array(
'id' => $source['data'][0]['id'],
'name' => $source['data'][0]['name'],
),
'subject' => " ".str_replace($strToRemove, "", $_POST['billing_myfield12'])." - "."Online marketing Duitsland",
'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" => [["voorstel_diensten"=>implode("-",$online_marketing_check)]],
);
return array_replace_recursive($sales_payload, $args);
}
如何使用:
$a = arrayClone(['status' => 'Closed', 'contact_id' => 5]);
$b = arrayClone(['status' => 'Closed Now', 'contact_id' => 20]);
你好,我正在尝试将 DRY 概念应用到我的程序中,该程序现在有 3000 多行代码,只是因为我一直在重复自己。
我有这个数组:
// prepare the sales payload
$sales_payload = array(
'organization_id' => $getOrg['data'][0]['id'],
'contact_id' => $contact_id,
'status' => 'Open',
'responsible_employee_id' => 'employee:50c76c15262b12d36d44e34a3f0f8c3d',
'source' => array(
'id' => $source['data'][0]['id'],
'name' => $source['data'][0]['name'],
),
'subject' => " ".str_replace($strToRemove, "", $_POST['billing_myfield12'])." - "."Online marketing Duitsland",
'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" => [["voorstel_diensten"=>implode("-",$online_marketing_check)]],
);
有没有一种方法可以只动态更改某些字段,而不是复制整个内容并手动更改它。
您可以声明一个函数来克隆数组并更改它
function arrayClone($args){
$sales_payload = array(
'organization_id' => $getOrg['data'][0]['id'],
'contact_id' => $contact_id,
'status' => 'Open',
'responsible_employee_id' => 'employee:50c76c15262b12d36d44e34a3f0f8c3d',
'source' => array(
'id' => $source['data'][0]['id'],
'name' => $source['data'][0]['name'],
),
'subject' => " ".str_replace($strToRemove, "", $_POST['billing_myfield12'])." - "."Online marketing Duitsland",
'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" => [["voorstel_diensten"=>implode("-",$online_marketing_check)]],
);
return array_replace_recursive($sales_payload, $args);
}
如何使用:
$a = arrayClone(['status' => 'Closed', 'contact_id' => 5]);
$b = arrayClone(['status' => 'Closed Now', 'contact_id' => 20]);