PHP - Concatenation of Array
PHP - Concatenation of Array

PHP – Concatenation of Array

PHP – Concatenation of Array

In PHP, concatenation is a fundamental operation used to combine variables, strings,  or arrays into a single thing. As we already know that concatenating strings and variables is a common practice, merging arrays is equally important, especially when dealing with data manipulation and processing.

Array concatenation involves merging two or more arrays into a single array, thereby combining their elements. This operation is particularly useful when we need to merge data from different sources or when you want to combine the contents of arrays dynamically.

In this article, we’ll explore various techniques and functions provided by PHP to concatenate arrays efficiently. We’ll cover both basic concatenation methods and more advanced techniques to handle array merging, ensuring you have the tools necessary to manipulate arrays effectively in your PHP projects.

Firstly, to concatenate Array in PHP, simply we can use array_merge() method. Let’s see an example:

 

$array1 = [1, 2, 3];
$array2 = [4, 5, 6];

$concatenatedArray = array_merge($array1, $array2);

print_r($concatenatedArray);

************************************************************
// OUTPUT:
************************************************************
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)

Now we will discuss about an LeetCode problem regarding concatenation of two Array.

Our given problem statement:

Given an integer array nums of length n, you want to create an array ans of length 2n, where:  ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n (0-indexed). Specifically, answer is the concatenation of two nums arrays. Return the array answer.

Problem statement explanation:

Example 1:

Input: nums = [1,2,1]
Output: [1,2,1,1,2,1]
Explanation: The array ans is formed as follows:
- ans = [nums[0],nums[1],nums[2],nums[0],nums[1],nums[2]]
- ans = [1,2,1,1,2,1]



Example 2:

Input: nums = [1,3,2,1]
Output: [1,3,2,1,1,3,2,1]
Explanation: The array ans is formed as follows:
- ans = [nums[0],nums[1],nums[2],nums[3],nums[0],nums[1],nums[2],nums[3]]
- ans = [1,3,2,1,1,3,2,1]

There are also some constraints involved, like:

Constraints:

  • n == nums.length
  • 1 <= n <= 1000
  • 1 <= nums[i] <= 1000

So let’s first see the solution and then analysis:

class Solution {
    /**
     * @param Integer[] $nums
     * @return Integer[]
     */
    function getConcatenation($nums) {
        $n = count($nums);
        $ans = array_fill(0, 2 * $n, 0);
        
        for ($i = 0; $i < $n; $i++) {
            $ans[$i] = $nums[$i];
            $ans[$i + $n] = $nums[$i];
        }
        
        return $ans;
    }
}

// Example usage:
$solution = new Solution();
$nums = [1,2,3];
$result = $solution->getConcatenation($nums);
print_r($result);

Analysis of the solution:

Class Definition:

Here, we define a PHP class named Solution. This class will contain the method to solve the given problem.

class Solution {

 

Method Definition:

This method getConcatenation() takes an array of integers $nums as input and returns an array of integers as output. The method is documented with the type hint @param Integer[] to indicate that the input parameter should be an array of integers, and @return Integer[] to indicate that the return value will also be an array of integers.

/**
* @param Integer[] $nums
* @return Integer[]
*/
function getConcatenation($nums) {

Calculating the Length of the Array:

 $n = count($nums);

We calculate the length of the input array $nums using the count() function and store it in the variable $n.

 

Creating the Result Array:

 $ans = array_fill(0, 2 * $n, 0);

We create an array named $ans using the array_fill() function. This function fills an array with a specified number of elements with a default value. Here, we fill the array with 2 * $n elements, all initialized with the value 0.

Concatenating the Arrays:

for ($i = 0; $i < $n; $i++) {
    $ans[$i] = $nums[$i];
    $ans[$i + $n] = $nums[$i];
}

We iterate over the elements of the input array $nums using a for loop. For each element at index $i, we set the corresponding elements in the concatenated array $ans. The first half of the concatenated array $ans is filled with the elements of $nums, and the second half is also filled with the elements of $nums.

Returning the result:

 return $ans;

Finally, we return the concatenated array $ans.

Example Usage:

// Example usage:
$solution = new Solution();
$nums = [1,2,3];
$result = $solution->getConcatenation($nums);
print_r($result);

This demonstrates how to use the Solution class to get the concatenated array for a given input array $nums. In this example, the input array is [1, 2, 3], and the output will be [1, 2, 3, 1, 2, 3], as per the concatenation rule.

 

That’s all for today. See you in next one.

 

Check Out More Resources:

Articles:
Website: https://laravelplug.com/
YouTube Channel: https://www.youtube.com/channel/UCnTxncHQcBSNs4RCuhWgF-A?sub_confirmation=1
WordPress Playlist: https://www.youtube.com/watch?v=8VvXzFAFwQU&list=PLVoVZqAh8dTLHZ51egvEU7dsOrRCSIMWx
Tools Playlist: https://www.youtube.com/watch?v=gTQoUn3BQkM&list=PLVoVZqAh8dTK-DWHBGWOEEvzvPQrgFne-

WordPress Tutorials: https://laravelplug.com/category/wordpress/
Laravel Tutorials: https://laravelplug.com/category/laravel/
PHP Tutorials: https://laravelplug.com/category/php/
SQL Tutorials: https://laravelplug.com/category/sql/
Various Tips: https://laravelplug.com/category/tips/
Useful Tools: https://laravelplug.com/category/tools/

Socials:

Twitter: https://twitter.com/LaravelPlug
Pinterest: https://www.pinterest.com/LaravelPlugCom/
Facebook: https://www.facebook.com/groups/1020759861842360
Mail: info@laravelplug.com

#PHP #array #leetcode

 

That’s All. Feel free to knock me. Thanks.

Editorial Staff

A Learner and trying to share what I learned!