array_combine
PHP array_combine()函数 (PHP array_combine() Function)
array_combine() function is an array function in PHP, it is used to create an array with two different arrays, where the first array contains keys and the second array contains values.
array_combine()函数是PHP中的一个数组函数,用于创建具有两个不同数组的数组,其中第一个数组包含键,第二个数组包含值。
Note: Both parameters should have an equal number of elements.
注意:这两个参数应具有相等数量的元素。
Syntax:
句法:
array_combine(array1, array2) : array
Here, array1 and array2 are two input arrays. Where, array1 contains "keys" and array2 contains "values". It returns a new array with "keys" and "values".
在这里, array1和array2是两个输入数组。 其中, array1包含“键”,而array2包含“值” 。 它返回带有“键”和“值”的新数组。
Examples:
例子:
Input:
$arr1 = array("101", "102", "103", "104", "105");
$arr2 = array("Amit Shukla", "Abhishek Jain", "Prerana", "Aleesha", "Prem");
Function call:
array_combine($arr1, $arr2);
Output:
Array
(
[101] => Amit Shukla
[102] => Abhishek Jain
[103] => Prerana
[104] => Aleesha
[105] => Prem
)
PHP code:
PHP代码:
<?php
//arr1 contains keys
$arr1 = array("101", "102", "103", "104", "105");
//arr2 contains values
$arr2 = array("Amit Shukla", "Abhishek Jain", "Prerana", "Aleesha", "Prem");
//combining arrays
$std = array_combine($arr1, $arr2);
//printing
print_r ($std);
?>
Output
输出量
Array
(
[101] => Amit Shukla
[102] => Abhishek Jain
[103] => Prerana
[104] => Aleesha
[105] => Prem
)
PHP Code (If number of elements of both arrays is not same)
PHP代码(如果两个数组的元素数不相同)
<?php
//arr1 contains keys
$arr1 = array("1", "2", "3");
//arr2 contains values
$arr2 = array("Amit", "Abhishek", "Prerana", "Aleesha");
//combining arrays
$std = array_combine($arr1, $arr2);
//printing
print_r ($std);
?>
Output
输出量
PHP Warning: array_combine(): Both parameters should have an equal
number of elements in /home/main.php on line 8
翻译自: https://www.includehelp.com/php/array_combine-function-with-example.aspx
array_combine