重写多维数组解析器

时间:2017-10-13 09:38:51

标签: php arrays parsing multidimensional-array

我正在尝试重写一个多维数组解析器函数,它对某些键有一些特殊情况。

将其重写为更易读且易于调试的最佳方法是什么?我应该为每个级别的缩进创建一个函数吗?

public function parse ($array_of_fields, $entries_details)
{
    foreach ($array_of_fields as $field_name => $field_value)
    {
        $email_details[$field_name] = array();
        if (!empty($field_value))
        {
            foreach ($entries_details as $entry_details)
            {
                foreach($entry_details['message'][0] as $entry_detail_key => $entry_detail_value)
                {
                    foreach ($field_value as $field)
                    {
                        if ($entry_detail_key == $field)
                        {
                            if ($entry_detail_value != "")
                            {
                                if ($entry_detail_key == "assigned_user_id")
                                {
                                    $assigned_user_data = $this->crm_api_post($entry_detail_value, "retrieve")["message"][0];
                                    $assigned_user_emails = array();
                                    foreach ($assigned_user_data as $key => $value)
                                    {
                                        if ($key == "email1" || $key == "email2" || $key == "secondaryemail")
                                        {
                                            if ($value != "")
                                            {
                                                array_push($assigned_user_emails, $value);
                                            }
                                        }
                                    }
                                    array_push($email_details[$field_name], implode(", ", $assigned_user_emails));
                                }
                                else
                                {
                                    array_push($email_details[$field_name], $entry_detail_value);
                                }
                            }
                        }
                    }
                }
            }
        }
        $email_details[$field_name] = implode(", ", $email_details[$field_name]);
    }

    return $email_details;
}

这是$ entries_details的一个例子:

array:1 [
  0 => array:5 [
    "total" => "1"
    "status" => "success"
    "code" => "200"
    "message" => array:1 [
      0 => array:51 [
        "salutationtype" => ""
        "firstname" => "Make them the same"
        "contact_no" => "CON521"
        "phone" => ""
        "lastname" => "YY"
        "mobile" => ""
        "account_id" => ""
        "homephone" => ""
        "leadsource" => "Cold Call"
        "otherphone" => ""
        "title" => "title"
        "fax" => ""
        "department" => ""
        "birthday" => ""
        "email" => "example1@yahoo.co.uk"
        "contact_id" => ""
        "assistant" => ""
        "secondaryemail" => "example2@yahoo.co.uk"
        "assistantphone" => ""
        "donotcall" => "1"
        "emailoptout" => "0"
        "assigned_user_id" => "19x100"
        "reference" => "1"
        "notify_owner" => "0"
        "createdtime" => "2017-09-01 10:22:25"
        "modifiedtime" => "2017-09-11 12:24:09"
        "modifiedby" => "19x1"
        "portal" => "0"
        "support_start_date" => ""
        "support_end_date" => ""
        "mailingstreet" => ""
        "otherstreet" => "XX"
        "mailingcity" => "XX"
        "othercity" => "XX"
        "mailingstate" => "XX"
        "otherstate" => "XX"
        "mailingzip" => "XX"
        "otherzip" => "XX"
        "mailingcountry" => "XX"
        "othercountry" => "XX"
        "mailingpobox" => "XX"
        "otherpobox" => "XX"
        "imagename" => ""
        "description" => ""
        "isconvertedfromlead" => "0"
        "tenant_id" => "1"
        "id" => "12x1891"
        "assigned_user_name" => "Worker Bee"
        "account_name" => ""
        "contact_name" => ""
        "modifiedby_name" => "INVALID"
      ]
    ]
  ]
]

$ array_of_fields的示例:

array:5 [
  "emailTo" => array:2 [
    0 => "email"
    1 => "secondaryemail"
  ]
  "emailCc" => []
  "emailBcc" => []
  "emailSubject" => []
  "emailBody" => []
]

上述输入的所需输出示例:

array:5 [
  "emailTo" => "example1@yahoo.co.uk, example2@yahoo.co.uk"
  "emailCc" => ""
  "emailBcc" => ""
  "emailSubject" => ""
  "emailBody" => ""
]

这是我现在正在尝试的东西(但不幸的是它不起作用):

错误在哪里?

public function getEmailDetailsFromCRM ($array_of_fields, $entries_details)
{
    foreach ($array_of_fields as $field_name => $field_value)
    {
        $email_details[$field_name] = array();

        $this->processEntryDetails($entries_details, $field_name, $field_value, $email_details);

        $email_details[$field_name] = implode(", ", $email_details[$field_name]);
    }

    return $email_details;
}

public function processEntryDetails($entries_details, $field_name, $field_value, $email_details)
{
    if (!empty($field_value))
    {
        foreach ($entries_details as $entry_details)
        {
            foreach($entry_details['message'][0] as $entry_detail_key => $entry_detail_value)
            {
                $this->getRequiredFields($field_name, $field_value, $entry_detail_key, $entry_detail_value, $email_details);
            }
        }
    }
}

public function getRequiredFields ($field_name, $field_value, $entry_detail_key, $entry_detail_value, $email_details)
{
    foreach ($field_value as $field)
    {
        if ($entry_detail_key == $field && $entry_detail_value != "")
        {
            $this->getDetailsOfRequiredFields($field_name, $entry_detail_key, $entry_detail_value, $email_details);
        }
    }
}

public function getDetailsOfRequiredFields ($field_name, $entry_detail_key, $entry_detail_value, $email_details)
{
    if ($entry_detail_key == "assigned_user_id")
    {
        $assigned_user_data = $this->crm_api_post($entry_detail_value, "retrieve")["message"][0];

        $assigned_user_emails = $this->getAssignedUserId($assigned_user_data);

        array_push($email_details[$field_name], implode(", ", $assigned_user_emails));
    }
    else
    {
        array_push($email_details[$field_name], $entry_detail_value);
    }
}

public function getAssignedUserId ($assigned_user_data)
{
    foreach ($assigned_user_data as $assigned_user_key => $assigned_user_value)
    {
        $assigned_user_emails = array();
        if (($assigned_user_key == "email1" || $assigned_user_key == "email2" || $assigned_user_key == "secondaryemail") && $assigned_user_value != "")
        {
            array_push($assigned_user_emails, $assigned_user_value);
        }

        return $assigned_user_emails;
    }
}

0 个答案:

没有答案