Pinq\ITraversable::indexBy PHP Method

indexBy() public method

All duplicate indexes will be associated with the first value for that index.
public indexBy ( callable $function ) : pinq\ITraversable
$function callable The projection function
return pinq\ITraversable
    public function indexBy(callable $function);

Usage Example

Beispiel #1
0
 /**
  * @dataProvider everything
  */
 public function testThatIdenticalNonIntegerOrStringMapToTheSameScalarKey(\Pinq\ITraversable $traversable, array $data)
 {
     foreach ([new \stdClass(), [], [1], fopen('php://input', 'r'), 3.22, null, true] as $nonStringOrInt) {
         $withNonIntOrString = $traversable->indexBy(function () use($nonStringOrInt) {
             return $nonStringOrInt;
         });
         $this->assertSame(empty($data) ? [] : [0 => reset($data)], $withNonIntOrString->asArray());
         if (is_object($nonStringOrInt)) {
             //Cloned object longer identical, should map to individual keys
             $withNonIntOrString = $traversable->indexBy(function () use($nonStringOrInt) {
                 return unserialize(serialize($nonStringOrInt));
             });
             $this->assertSame(array_values($data), $withNonIntOrString->asArray());
         }
     }
 }
All Usage Examples Of Pinq\ITraversable::indexBy