Formatting Arrays

From Nephtali Documentation

Jump to: navigation, search

Nephtali provides the function n\arr\format() to simplify the formatting of values in an array or an array of arrays. The function accepts as the first argument the array to format, and as the second argument the associative array of keys and their corresponding formatting functions. Each formatting function must accept a value as an argument and return the formatted value. The array can containing any level of nesting.

Example

In the example below, the 'date' field is formatted for inclusion in a DB and the 'password' field is encrypted.

$users = array(
     array(
          'name' => 'George Jones',
          'date' = > '11/2/2009',
          'password' => 'superduper'
     ),
     array(
          'name' => 'William James',
          'date' = > '1/7/2008',
          'password' => 'Fito'
     )	
);

$formatted_users = n\arr\format(
     $users,
     $key_functions = array(
          'date' => function($val){return date('Y-m-d', strtotime($val));},
          'password' => function($val){return n\security\encrypt($val);}
     )
);
Personal tools