src/Support/Collection.php
/**
     * Key an associative array by a field or using a callback.
     *
     * @param callable|string  $keyBy
     * @return static
     */
Key an associative array by a field or using a callback.
@param callable|string  $keyBy
@return static
Key an associative array by a field or using a callback . @param callable|string $keyBy @return static
public function keyBy($keyBy)
    {
        $keyBy = $this->valueRetriever($keyBy);
        $results = [];
        foreach ($this->items as $key => $item) {
            $resolvedKey = $keyBy($item, $key);
            if (is_object($resolvedKey)) {
                $resolvedKey = (string) $resolvedKey;
            }
            $results[$resolvedKey] = $item;
        }
        return new static($results);
    }
public function keyBy ( $ keyBy ) { $ keyBy = $ this -> valueRetriever ( $ keyBy ) ; $ results = [ ] ; foreach ( $ this -> items as $ key => $ item ) { $ resolvedKey = $ keyBy ( $ item , $ key ) ; if ( is_object ( $ resolvedKey ) ) { $ resolvedKey = ( string ) $ resolvedKey ; } $ results [ $ resolvedKey ] = $ item ; } return new static ( $ results ) ; }
Key an associative array by a field or using a callback.
Key an associative array by a field or using a callback .