array_merge()
and array_combine()
are two different array functions in PHP that serve different purposes:
array_merge()
: This function is used to merge two or more arrays into a single array. It takes multiple arrays as arguments and returns a new array that contains all the elements from the input arrays. Here's an example:
s
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];
$result = array_merge($array1, $array2);
print_r($result);
// Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )
In the example above, array_merge()
is used to merge $array1
and $array2
into a single array $result
. The resulting array contains all the elements from both input arrays.
array_combine()
: This function is used to create a new array by combining two arrays, one containing keys and the other containing values. It takes two arrays as arguments, with the first array providing keys and the second array providing values. Here's an example:
s
$keys = ['name', 'age', 'city'];
$values = ['John Doe', 25, 'New York'];
$result = array_combine($keys, $values);
print_r($result);
// Output: Array ( [name] => John Doe [age] => 25 [city] => New York )
In the example above, array_combine()
combines the $keys
array and the $values
array to create a new array $result
. The values in the $keys
array become the keys, and the corresponding values from the $values
array become the values in the resulting array.
To summarize, array_merge()
is used to merge multiple arrays into one, while array_combine()
is used to combine two arrays into a single array, with one array providing keys and the other providing values.
No comments:
Post a Comment