示例#1
0
 /**
  *
  * @test
  * @dataProvider providerMap
  */
 public function testMap($method, $source, $keys, $expected)
 {
     $this->assertSame($expected, Arr::map($method, $source, $keys));
 }
示例#2
0
 /**
  * Recursive version of [array_map](http://php.net/array_map), applies one or more
  * callbacks to all elements in an array, including sub-arrays.
  *
  *     // Apply "strip_tags" to every element in the array
  *     $array = Arr::map('strip_tags', $array);
  *
  *     // Apply $this->filter to every element in the array
  *     $array = Arr::map(array(array($this,'filter')), $array);
  *
  *     // Apply strip_tags and $this->filter to every element
  *     $array = Arr::map(array('strip_tags',array($this,'filter')), $array);
  *
  * [!!] Because you can pass an array of callbacks, if you wish to use an array-form callback
  * you must nest it in an additional array as above. Calling Arr::map(array($this,'filter'), $array)
  * will cause an error.
  * [!!] Unlike `array_map`, this method requires a callback and will only map
  * a single array.
  *
  * @param  mixed $callbacks array of callbacks to apply to every element in the array
  * @param  array $array     array to map
  * @param  array $keys      array of keys to apply to
  * @return array
  */
 public static function map($callbacks, $array, $keys = null)
 {
     foreach ($array as $key => $val) {
         if (is_array($val)) {
             $array[$key] = Arr::map($callbacks, $array[$key]);
         } elseif (!is_array($keys) || in_array($key, $keys)) {
             if (is_array($callbacks)) {
                 foreach ($callbacks as $cb) {
                     $array[$key] = call_user_func($cb, $array[$key]);
                 }
             } else {
                 $array[$key] = call_user_func($callbacks, $array[$key]);
             }
         }
     }
     return $array;
 }