Exemplo n.º 1
0
/**
 * Calls a transformation function for each element of an array.
 * The function will receive one argument for each specified column.
 * It should return an array/object that will replace the original array element.
 * Unlike array_map, the original keys will be preserved.
 *
 * @param array    $data
 * @param array    $cols
 * @param callable $fn
 *
 * @return array A transformed copy of the input array.
 */
function array_mapColumns(array $data, array $cols, callable $fn)
{
    $o = [];
    foreach ($data as $k => $r) {
        $o[$k] = call_user_func_array($fn, array_fields($r, $cols));
    }
    return $o;
}
Exemplo n.º 2
0
 /**
  * Extracts the values with the given keys from a given array, in the same order as the key list.
  *
  * @param array $keys A list of keys to be extracted.
  * @param mixed $def  An optional default value to be returned for non-existing keys.
  * @return PowerArray Self, for chaining.
  * @see PowerArray::extract
  */
 function fields(array $keys, $def = null)
 {
     $this->A = array_fields($this->A, $keys, $def);
     return $this;
 }