PHP array_combine() Function with Example

PHP array_combine() Function with Example

PHP array_combine() Function: Here, we will find out about the array_combine() work with example in PHP.

PHP array_combine() Function

array_combine() work is an exhibit work in PHP, it is utilized to make a cluster with two unique clusters, where the primary exhibit contains keys and the subsequent exhibit contains values.

Note: Both parameters ought to have an equivalent number of components.

Syntax:

    array_combine(array1, array2) : array

Here, array1 and array2 are two information clusters. Where array1 contains “keys” and array2 contains “values“. It restores another cluster with “keys” and “values“.

Example:

    Input:
    $arr1 = array("101", "102", "103", "104", "105");
    $arr2 = array("Kishan Kaushik", "Durgesh Kaushik", "Aakash", "Akansha", "Manish");

    Function call:
    array_combine($arr1, $arr2);

    Output:
    Array
    (
        [101] => Kishan Kaushik
        [102] => Durgesh Kaushik
        [103] => Aakash
        [104] => Akansha
        [105] => Manish
    )

PHP Code:

<?php
    //arr1 contains keys
    $arr1 = array("101", "102", "103", "104", "105");
    //arr2 contains values
    $arr2 = array("Kishan Kaushik", "Durgesh Kaushik", "Aakash", "Akansha", "Manish");
    
    //combining arrays
    $std = array_combine($arr1, $arr2);
    //printing
    print_r ($std);
?>

Output:

Array
(
    [101] => Kishan Kaushik
    [102] => Durgesh Kaushik
    [103] => Aakash
    [104] => Akansha
    [105] => Manish
)

PHP Code (If a number of components of the two exhibits aren’t the same):

<?php
    //arr1 contains keys
    $arr1 = array("1", "2", "3");
    //arr2 contains values
    $arr2 = array("Kishan", "Durgesh", "Aakash", "Akansha");
    
    //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

Recent Posts

error: Alert: Content is protected!!