Required : Input array of which kyes will be retained.
$input_array2
Required : Input array of which values will be retained.
Returns an array with keys of first array and values of second array.
Returns FALSE if both arrays are not having equal number of elements.
Example 1: Handling Mismatched Array Lengths
<?php
$keys = ['name', 'email'];
$values = ['John Doe', 'john@example.com', 25]; // Extra value
// This will trigger a warning because the arrays are not of equal length
$result = @array_combine($keys, $values);
if ($result === false) {
echo "Error: Arrays must have the same length!";
} else {
print_r($result);
}
?>
Output
Error: Arrays must have the same length!
Example 2: Real-World Use Case - Combining Form Labels and User Input
<?php
$labels = ['Name', 'Email', 'Age'];
$userInput = ['Alice', 'alice@example.com', 28];
// Combining form labels with user input
$formData = array_combine($labels, $userInput);
print_r($formData);
?>