How to combine multiple arrays. We will see this in this article. So what is an array and why do we need to combine arrays?
What is Array? Why Do We Need to Combine?
Array means many variables of the same data type. The variables of the array are like a list with a sequential number. We can represent an array as follows.
numbers = [1,2,3,4,5];
letters = ['a', 'b', 'c'];
...
So why do we feel the need to combine? Sometimes we may want to display all the data collectively, or a column in the database keeps records in the form of an array and we may want to pull the data, combine it and save it again in an update operation. For these reasons, it may be necessary to merge the series. Now let’s see how to combine two or more arrays in some programming languages.
Merging multiple arrays in Javascript
In Javascript, concatenation of two or more arrays is done with the concat function. Other arrays to be concatenated with a string are written into the concat function.
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = ['g', 'h', 'i'];
const combinedArray = array1.concat(array2, array3);
console.log(combinedArray);
Merging multiple arrays in Python
In Python, concatenation of two or more arrays can be done with the + operator. Arrays to be combined are combined with the + sign, just like addition.
array1 = ['a', 'b', 'c']
array2 = ['d', 'e', 'f']
array3 = ['g', 'h', 'i']
combined_array = array1 + array2 + array3
print(combined_array)
Merging multiple arrays in PHP
Merging two or more arrays in PHP is done with the array_merge function. The arrays you want to combine are written into this function and the merging process is performed.
$array1 = ['a', 'b', 'c'];
$array2 = ['d', 'e', 'f'];
$array3 = ['g', 'h', 'i'];
$combinedArray = array_merge($array1, $array2, $array3);
echo $combinedArray;
Merging multiple arrays in C#
In C#, we can combine two or more arrays by putting the arrays into a List and combining them with AddRange.
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
string[] array1 = { "a", "b", "c" };
string[] array2 = { "d", "e", "f" };
string[] array3 = { "g", "h", "i" };
List combinedArray = new List(array1);
combinedArray.AddRange(array2);
combinedArray.AddRange(array3);
foreach (string item in combinedArray)
{
Console.WriteLine(item);
}
}
}
We can also do it with the concat function.
int[] array1 = new int[] { 1, 2, 3 };
int[] array2 = new int[] { 4, 5, 6 };
int[] result = array1.Concat(array2).ToArray();
Console.WriteLine("Result Array: [{0}]", string.Join(", ", result));
Merging multiple arrays in C++
In C++, concatenation of two or more arrays can be done by using the insert(), begin() and end() functions together.
#include
#include
int main() {
std::vector array1 = {'a', 'b', 'c'};
std::vector array2 = {'d', 'e', 'f'};
std::vector array3 = {'g', 'h', 'i'};
std::vector combinedArray;
combinedArray.insert(combinedArray.end(), array1.begin(), array1.end());
combinedArray.insert(combinedArray.end(), array2.begin(), array2.end());
combinedArray.insert(combinedArray.end(), array3.begin(), array3.end());
for (char item : combinedArray) {
std::cout << item << std::endl;
}
return 0;
}
Merging multiple arrays in C
In C, concatenation of two or more arrays can be done with the memcpy() function.
#include
#include
int main() {
char array1[] = {'a', 'b', 'c'};
char array2[] = {'d', 'e', 'f'};
char array3[] = {'g', 'h', 'i'};
char combinedArray[9];
memcpy(combinedArray, array1, 3);
memcpy(combinedArray + 3, array2, 3);
memcpy(combinedArray + 6, array3, 3);
for (int i = 0; i < 9; i++) {
printf("%c\n", combinedArray[i]);
}
return 0;
}
Merging multiple arrays in Swift
In Swift, concatenation of two or more arrays can be done with the + operator. Arrays to be combined are combined with the + sign, just like addition.
let array1 = ["a", "b", "c"]
let array2 = ["d", "e", "f"]
let array3 = ["g", "h", "i"]
let combinedArray = array1 + array2 + array3
for item in combinedArray {
print(item)
}
In each language, the new array formed by combining two arrays is then printed on the screen by accessing the elements with the loop structures of each language. In this article, we saw how to combine multiple arrays. See you in our other articles…
Share
Follow
Eğitimin, Eğlencenin ve Haberin Sitesi TEKNOKODİ