Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - Ds Sequence::unshift() Function



The PHP Ds\Sequence::unshift() function is used to add values to the front of a sequence.

This function allows you to add multiple values to the front of a sequence at a time by moving all the values forward to make space for the newly added values.

Syntax

Following is the syntax of the PHP Ds\Sequence::unshift() function −

public abstract void Ds\Sequence::unshift([ mixed $values ] )

Parameters

Following is the parameter of this function −

  • values − Single or multiple values need to be added in front of a sequence.

Return value

This function does not return any value.

Example 1

The following program demonstrates the usage of the PHP Ds\Sequence::unshift() function −

<?php
   $seq = new \Ds\Vector([1, 2, 3]);
   echo "The sequence elements are: \n";
   print_r($seq);
   $val = 0;
   echo "The given value: ".$val;
   $seq->unshift($val);
   echo "\nThe sequence after unshift elements: \n";
   print_r($seq);
?>

Output

The above program produces the following output −

The sequence elements are:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)
The given value: 0
The sequence after unshift elements:
Ds\Vector Object
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
)

Example 2

Adding multiple values in front of a sequence at a time.

Following is another example of the PHPDs\Sequence::unshift()function. We use this function to add the specified elements 'c', 'd', and 'e' at the front of this vector (['a', 'b']) −

<?php
   $seq = new \Ds\Vector(['a', 'b']);
   echo "The sequence elements are: \n";
   print_r($seq);
   $val1 = 'c';
   $val2 = 'd';
   $val3 = 'e';
   echo "The given values are: ".$val1.", ".$val2.", ".$val3;
   $seq->unshift($val1, $val2, $val3);
   echo "\nThe sequence after unshift elements: \n";
   print_r($seq);
?>

Output

After executing the above program, the following output will be displayed −

The sequence elements are:
Ds\Vector Object
(
    [0] => a
    [1] => b
)
The given values are: c, d, e
The sequence after unshift elements:
Ds\Vector Object
(
    [0] => c
    [1] => d
    [2] => e
    [3] => a
    [4] => b
)
php_function_reference.htm
Advertisements