如何以更清洁的方式格式化数组数据?

I have this method:

private function formatCliendCardData($data) {
    $formatedData = array();

    $formatedData['first_name'] = trim($data['name']);
    $formatedData['last_name'] = trim($data['surname']);
    $formatedData['date_of_birth'] = $data['birth_year'] . '-' . sprintf("%02d", $data['birth_month_id']) . '-' . sprintf("%02d", $data['birth_day']); //[yyyy-mm-dd]
    $formatedData['sex'] = ($data['sex_id'] == 's1'? 'm' : 'f');
    $formatedData['county'] = 'Latvija'; //@TODO get real data
    $formatedData['post_index'] = (int) preg_replace( '/[^0-9]/', '', $data['post_index']);
    $formatedData['city'] = trim($data['city']);
    $formatedData['street'] = trim($data['street']);
    $formatedData['house_nr'] = trim($data['house_nr']);
    $formatedData['phone'] = trim($data['phone']);
    $formatedData['email'] = trim($data['email']);

    return $formatedData;
}

I run in this problem from time to time. I have big method that just reformats data and returns it. It looks ugly. There is few other aproaches Im aware -- just make foreach loop, but there are exeptions, so i need make 'ifs' anyway. What is better way to reformat such data in your opinon?

I aggree with helloei,

create a class to handle all the formating and validation in the setter.

class Person
{
 public function setName($name)
 {
   $this->name = trim($name);
 }
 ...
}

and then create the object in your function:

private function formatCliendCardData($data) {
    $person = new Person();
    $person->setName($data['name`])
    ...

if you have highly custom condition to reformat some datas i think you don't have any other solution that apply this condition manually with some if/else or other custom loop.

if else, you have some unified condition to reformat you can aggregate this and apply in batch at your datas. In other word for my opinion the only other solution are: generalize conditions of your reformatting operations and apply this at your data.

this is Object Oriented approach

IMHO In those cases you have to reduce and clean your code. Often I use an helper function that allows me to split data and formatting rules.

// helper function (OUTSITE YOUR CLASS)
function waterfall($input=NULL,$fns=array()){
   $input = array_reduce($fns, function($result, $fn) {
     return $fn($result);
   }, $input);
   return $input;
}

// your formatting rules
$rules = array(
    'first_name' => array('trim'),
    'last_name' => array('trim'),
    'date_of_birth' => array(
        'f0' => function($x){
            return sprintf("%s-%02d-%02d",
                            $x['birth_year'],
                            $x['birth_month_id'],
                            $x['birth_day']
                   );
        },
        'trim'
    ),
    'sex' => array(
        'f0' => function($x){
            if($x['sex_id'] == 's1'){
                return 'm';
            }else{
                return 'f';
            }
        }
    ),
    'post_index' => array(
        'f0' => function($x){ 
                   return (int) preg_replace('/[^0-9]/', NULL, $x); 
                },
        'trim'
    ),
    'city' => array('trim'),
    'email' => array('strtolower','trim')
);

// your data
$data = array(
    'first_name' => '  Andrea',
    'last_name' => 'Ganduglia ',
    'date_of_birth' => array(
        'birth_year' => '1899',
        'birth_month_id' => 5,
        'birth_day' => 7
    ),
    'post_index' => 'IT12100',
    'city' => 'Rome',
    'email' => 'USER@DOMAIN.tld '
);

// put all together
$formatedData = array();
foreach($data as $k => $v){
    $formatedData[$k] = waterfall($v,$rules[$k]);
}

// results
print_r($formatedData);
/*
Array
(
    [first_name] => Andrea
    [last_name] => Ganduglia
    [date_of_birth] => 1899-05-07
    [post_index] => 12100
    [city] => Rome
    [email] => user@domain.tld
)
*/