Ouzo\Utilities\Arrays::uniqueBy PHP 메소드

uniqueBy() 공개 정적인 메소드

Example: $a = new stdClass(); $a->name = 'bob'; $b = new stdClass(); $b->name = 'bob'; $array = [$a, $b]; $result = Arrays::uniqueBy($array, 'name'); Result: Array ( [0] => $b )
public static uniqueBy ( array $elements, $selector ) : array
$elements array
$selector
리턴 array
    public static function uniqueBy(array $elements, $selector)
    {
        return array_values(self::toMap($elements, Functions::extractExpression($selector)));
    }

Usage Example

예제 #1
0
 /**
  * @test
  */
 public function shouldReturnObjectsUniqueByNestedField()
 {
     //given
     $category = new Category(array('name' => 'cat1'));
     $product1 = new Product(array('name' => 'bob'));
     $product1->category = $category;
     $product2 = new Product(array('name' => 'john'));
     $product2->category = $category;
     $array = array($product1, $product2);
     //when
     $uniqueByName = Arrays::uniqueBy($array, 'category->name');
     //then
     Assert::thatArray($uniqueByName)->hasSize(1);
 }