-3

I have two string :

$var1 = "1,2";
$var2 = "5,5";

I want output like: 5:1 5:2

I have tried explode() with array_combine() but it gives output like 5:2

My php code:

$res =  array_combine(explode(',', $var2), explode(',', $var1));

foreach($res as $key=>$val) {
 echo "$key:$val ";
}
Omi
  • 3,954
  • 5
  • 21
  • 41

1 Answers1

1

Array keys must be unique and with your code you have two number 5, so you will get the second one. You could loop one array and access the other with the same key:

$array1 = explode(',', $var1);
$array2 = explode(',', $var2);

foreach($array2 as $key => $val) {
    echo "$val:{$array1[$key]} ";
}
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87