PHP中的DRY数组

问题描述 投票:0回答:2

您好,我正在尝试将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)]],

);

有没有一种方法可以仅动态更改某些字段,而不是复制整个内容并手动更改。

php arrays dry
2个回答
2
投票

您可以声明一个克隆数组并对其进行更改的函数

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]);

0
投票

我不确定您的意思,是否要复制数组并将其粘贴到另一个文件中?还是要在此特定文件中更改某些值。到目前为止,我所了解的是:您想更改一个单词的所有出现,Crtl + H会做到这一点。否则,选择单词并右键单击它,然后单击“更改所有出现的单词”。

© www.soinside.com 2019 - 2024. All rights reserved.