HomeLanguagesPHP | DsDeque merge() Function

PHP | Ds\Deque merge() Function

The Ds\Deque::merge() function is an inbuilt function in PHP which is used to return the merged Deque after merging all the elements of one Deque with another by adding all the values into a copy and returns that copy.

Syntax:

public Ds\Deque::merge( $values ) : Ds\Deque

Parameters: This function accepts single parameter $values which contains the values to be merged with the calling Deque.

Return Value: This function returns a Deque which contains all the elements of both the Deque.

Below programs illustrate the Ds\Deque::merge() function in PHP:

Program 1:




<?php
  
// Declare a deque
$deck = new \Ds\Deque([10, 20, 30, 40, 50, 60]);
  
echo("Elements of first deque\n");
  
// Display the deque Elements
print_r($deck);
  
// Declare another deque
$deck2 = new \Ds\Deque([70, 80, 90, 100]);
  
echo("\nElements of second deque\n");
print_r($deck2);
  
echo("\nMerged deque elements\n");
  
// Merge the both deque
print_r($deck->merge($deck2));
  
?>


Output:

Elements of first deque
Ds\Deque Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
    [5] => 60
)

Elements of second deque
Ds\Deque Object
(
    [0] => 70
    [1] => 80
    [2] => 90
    [3] => 100
)

Merged deque elements
Ds\Deque Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
    [5] => 60
    [6] => 70
    [7] => 80
    [8] => 90
    [9] => 100
)

Program 2:





Output:

Elements of first deque
Ds\Deque Object
(
    [0] => neveropen
    [1] => for
    [2] => neveropen
)

Elements of second deque
Ds\Deque Object
(
    [0] => practicing
    [1] => data
    [2] => structures
)

Merged deque elements
Ds\Deque Object
(
    [0] => neveropen
    [1] => for
    [2] => neveropen
    [3] => practicing
    [4] => data
    [5] => structures
)

Reference: http://php.net/manual/en/ds-deque.merge.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32715 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7092 POSTS0 COMMENTS
Nicole Veronica
12231 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12314 POSTS0 COMMENTS
Shaida Kate Naidoo
7209 POSTS0 COMMENTS
Ted Musemwa
7473 POSTS0 COMMENTS
Thapelo Manthata
7193 POSTS0 COMMENTS
Umr Jansen
7173 POSTS0 COMMENTS