duangang4940 2017-04-14 19:47
浏览 82

array_combine不能使用重复值

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 ";
}
  • 写回答

1条回答 默认 最新

  • drwkqwa82140 2017-04-14 19:55
    关注

    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]} ";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料