Search code examples
phparraysarray-combine

Why array combine is not working


I have two strings and i want to concatenate string like john+smith to jsomhinth. i did this but array_combine not showing the result set. array_combine is not working here

What is the use of array_combine?

<? php

//variable that store two string
    $a ='JOHN';
    $b='SMITH';
    $val=str_split($a,1);
    $val1=str_split($b,1);
    //print_r($val1);
    //print_r($val);
    $c=array_combine($val,$val1);
    print_r($c);
?>

This the code i tried i got two array with key and id i want connect the key with array combine and want this output is there any solution two concatenate two strings like that???

And want to know that why array_combine not work there and what is the difference between array_combine and array merge.


Solution

  • For john+smith=jsomhinth you can try this -

    $a ='JOHN';
    $b='SMITH';
    $val=str_split($a,1);
    $val1=str_split($b,1);
    // Merge the array values pairwise
    $str_array = array_map(function($x, $y) {
        return ($x . $y);
    }, $val, $val1);
    
    $str = '';
    // Concatenate the values
    foreach($str_array as $s)
    {
       $str .= $s;
    }
    

    OUTPUT

    JSOMHINTH
    

    Code example