Get the first n elements.

@param array $elements
@param integer $amount
@return mixed|array
320853
public function first(array $elements, $amount = 1)
    {
        $elements = array_slice($elements, 0, $amount);

        return count($elements) == 1 ? reset($elements) : $elements;
    }
309125
public function getItems($count = null, $start = 0)
    {
        $matches = $this->gateway->getAll($this->table);
        $matches = $this->applyConditions($matches);
        $matches = $this->applyOrdering($matches);

        if (!empty($this->columns)) {
            $result = $this->applyColumns($matches);
        } else {
            $result = $this->castToEntity($matches);
        }

        return array_slice($result, $start, $count);
    }
660107
public function first($fields = null)
    {
        $r = $this->select($fields)->limit(1)->fetch(true);
        return $r ? $r[0] : null;
    }
1133064
private function computeElementsToJson(array $elements)
    {
        $result = [];
        foreach ($elements as $key => $element) {
            $result[$key] = null;
            if ($element instanceof AbstractContent) {
                $result[$key] = [
                    'uid'  => $element->getUid(),
                    'type' => $element->getContentType(),
                ];
            } elseif (is_scalar($element)) {
                $result[$key] = $element;
            } elseif (is_array($element)) {
                $result[$key] = $this->computeElementsToJson($element);
            }
        }

        return $result;
    }
1076146
public function take($n)
    {
        return new Enumerator(
            function () use($n) {
                foreach ($this->each() as $k => $v) {
                    yield $k => $v;
                    if (-- $n < 1)
                        return;
                }
            }
        );
    }
90584
public function lastIndexOf($element) {
        for ($i = (count($this->_elements) - 1); $i > 0; $i--) {
            if ($element == $this->get($i)) { return $i; }
        }
    }
1167595
public function first($columns = [])
    {
        $results = $this->select($columns)->limit(1)->get();

        // Since results may be returned inside an array if `raw()`
        // is specified, then we'll use our array helper
        // to retrieve the first result.
        return Arr::get($results, 0);
    }
602964
public function getNFirst($n)
    {
        $it = new \ArrayIterator();
        $j = 0;
        foreach ($this as $file) {
            if ($j == $n) {
                break;
            }
            $j++;
            $it->append($file);
        }

        $finder = new static();
        $finder->append($it);

        return $finder;
    }
1147586
public function limit($offset, $amount = null)
    {
        $this->queryBuilder->limit($offset, $amount);
        return $this;
    }
860271
public function first($columns = ['*'])
    {
        $this->query->take(1);

        $results = $this->get($columns);

        if ($results instanceof Collection) {
            return $results->first();
        }

        return \count($results) > 0 ? \reset($results) : null;
    }
627133
public function pop(int $amount = 1):  ? array
    {
        $t = microtime(true);
        if ($amount > 1) {
            $this->Driver->multi(Redis::MULTI); //采用事务
            $this->Driver->lRange($this->__name, 0, $amount - 1);
            $this->Driver->lTrim($this->__name, $amount, -1);
            $data = $this->Driver->exec()[0]; //只取第一个操作的结果
            if (empty($data)) {
                $data = false;
            } else {
                foreach ($data as $key => $value) {
                    $data[$key] = unserialize($value);
                }
            }
        } else {
            $data = $this->Driver->lPop($this->__name);
            if ($data !== false) {
                $data = [unserialize($data)];
            }
        }
        if ($this->Debug) {
            $this->Debug->pop($this->__name, $amount, microtime(true) - $t, $this->index, $data !== false);
        }
        return $data === false ? null : $data;
    }
668079
public function setRows(int $amount)
    {
        if (!$amount) {
            throw new InvalidArgumentException('Rows amount must be set');
        }

        if ($this->rows === $amount) {
            return $this;
        }

        if ($this->rows < $amount) {
            // Add rows
            for ($i = $amount - $this->rows; $i > 0; $i--) {
                $this->structure[] = $this->columns ? array_fill(0, $this->columns, $this->cell_example) : [];
            }
        } else {
            // Remove rows from the end
            $this->structure = \array_slice($this->structure, 0, $amount);
        }

        $this->rows = $amount;

        return $this;
    }
426965
public function incrementStockCount(string $productNumber, int $amount) : array
    {
        Assert::that($productNumber)->minLength(1, 'The length of $productNumber has to be > 0');

        return $this->incrementOrDecrementStockCount($productNumber, abs($amount));
    }
759354
public function get_entries($start=0, $limit=10) {
        global $DB;

        if ($this->entries === null) {
            if ($sqlarray = $this->get_entry_fetch_sql(false, 'created DESC')) {
                $this->entries = $DB->get_records_sql($sqlarray['sql'], $sqlarray['params'], $start, $limit);
                if (!$start && count($this->entries) < $limit) {
                    $this->totalentries = count($this->entries);
                }
            } else {
                return false;
            }
        }

        return $this->entries;
    }
897925
public static function random(array $array, int $amount = null)
    {
        if (is_null($amount)) {
            return $array[array_rand($array)];
        }

        $keys = array_rand($array, $amount);

        $results = [];

        foreach ((array) $keys as $key) {
            $results[] = $array[$key];
        }

        return $results;
    }
35581
protected function getHydrationElements($elements)
    {
        $elements = ($elements ?: $this->config->get('hydrate.elements'));

        if (is_string($elements) || is_numeric($elements)) {
            return [$elements => true];
        }

        return $this->checkHydrationElements($elements);
    }
806042
public function getVatAmounts()
    {
        $amounts = [];

        foreach ($this->getOrderElements() as $orderElement) {
            $rate = $orderElement->getVatRate();
            $amount = (string) $orderElement->getVatAmount();

            if (isset($amounts[$rate])) {
                $amounts[$rate]['amount'] = bcadd($amounts[$rate]['amount'], $amount);
            } else {
                $amounts[$rate] = [
                    'rate' => $rate,
                    'amount' => $amount,
                ];
            }
        }

        return $amounts;
    }