-2

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.

Mohammad
  • 21,175
  • 15
  • 55
  • 84
  • um... have your read the docs for [`array_combine(..)`](http://php.net/manual/ro/function.array-combine.php) itself..? it will result keyed arrays. if you want to concatenate string, use dot (`.`), ie. `$a . $b`. – Bagus Tesa May 02 '17 at 08:38
  • 2
    Because array_combine is a function for something completely different; for creating an array with a predefined set of keys and a predefined set of values – Mark Baker May 02 '17 at 08:38
  • Just, what? Can you reword your question? It doesn't make sense. If you want to know the difference between array_merge and array_combine, read the docs. – Jonnix May 02 '17 at 08:38
  • i want to concatenate two string charecter by charecter john+smith=jsomhinth – Gaurav Pandey May 02 '17 at 08:40
  • In `array_combine` both parameters should have an equal number of elements. – hatef May 02 '17 at 08:56

2 Answers2

2
$a = "JOHN";
$b = "SMITH";
$c = "";
$length = strlen($a)> strlen($b) ? strlen($a) : strlen($b);

echo "Given String is ". $a."--".$b."<br>"."Output : ";

for ($i = 0; $i <= $length; $i++) {

   $c.= substr($a, $i,1);    
   $c.= substr($b, $i,1);
}
echo $c;
Rishi
  • 473
  • 4
  • 13
  • If you want to store in a variable you can connect it to a variable and display it. – Rishi Nov 06 '17 at 13:27
  • Thx for your 1st answer on SO. Could you edit your answer to show how to get the wanted result stored in `c` as attempted in the question? I saw your comment but maybe you can explain *how* to 'connect it to a variable'? – mschilli Nov 06 '17 at 13:36
  • @mschilli try it now..! – Rishi Nov 06 '17 at 14:28
1

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

Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
  • `array_combine` does not work that way. You can check http://php.net/manual/en/function.array-combine.php for reference – Sougata Bose May 02 '17 at 08:55
  • It's actually working @GauravPandey, if you want to see the output add a `var_dump($str)` at the end. – hatef May 02 '17 at 09:05