Skip to content
Related Articles
Open in App
Not now

Related Articles

PHP Ds\Sequence Functions Complete Reference

Improve Article
Save Article
  • Last Updated : 25 Jan, 2023
Improve Article
Save Article

A Sequence is used to arrange the values in a single and linear dimension way. In some languages, the sequence refers to the List. The sequence is similar to the array which is used as incremental integer keys are listed below:

  • The index value of the sequence are [0, 1, 2, …, size – 1], where size represents the size of the array.
  • The sequence elements are accessed by using index value so it allows to access values by index in the range [0, size – 1].
  • The DS\Sequence is the efficient Data Structure in PHP 7. It is an alternative of the array.

Requirements: PHP 7 is required for both extension and the compatibility polyfill. 

Installation: The easiest way to install data structure by using the PECL extension.

pecl install ds

Syntax:

abstract public Ds\Sequence:: Function()

Example: Below programs illustrate the Ds\Sequence::capacity() function in PHP:

PHP




<?php
 
// Declare a sequence
$seq = new \Ds\Vector();
 
// Use capacity() function
var_dump($seq->capacity());
 
// Push element in sequence
$seq->push(...range(1, 100));
 
// Use capacity() function
var_dump($seq->capacity());
 
// Pop element in sequence
$seq->pop();
 
// Use capacity() function
var_dump($seq->capacity());
 
?>

Output:

int(8)
int(100)
int(100)

The complete list of data structure DS\SequenceFunctuon is given below:

 

 Functions 

Description

allocate() Allocate enough memory for required capacity.
apply() Updates all value of sequence by applying a callback Function to each value.
capacity() Return the current capacity of sequence.
contains() Check the given value exists in the sequence or not.
filter() Create new sequence using filter kk.
find() Find the value from the sequence. 
first() Return the first element from the sequence.
get() Returns the value at the given index.
insert() Insert value in the sequence at the given index.
join() Join all values as strings.
last() Return the last element from the sequence.
map() Returns the result after applying a callback Function to each value
merge() Returns a sequence after adding all given values to the sequence.
pop() Removes the last value from the sequence and returns it.
push() Adds values to the end of the sequence.
reduce() Reduce the sequence to a single value using a callback Function
remove() Remove and return a value by index.
reverse() Reverse the sequence in place.
reversed() Return the reverse copy of the sequence element.
rotate() Rotate the sequence element by a given number of rotations.
set() Updates a value at a given index.
shift() Remove the first element from the sequence and return it.
slice() Return the subsequence of given range.
sort() Sort the sequence element in the same place. 
sorted() Return the sorted copy of sequence element. 
sum() Return the sum of all values of the sequence.
unshift() Add values to the font of the sequence.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!