draw62188 2013-04-16 04:18
浏览 17
已采纳

使用php在一对2中分割逗号分隔的字符串

I have a string having 128 values in the form of :

1,4,5,6,0,0,1,0,0,5,6,...1,2,3.

I want to pair in the form of :

(1,4),(5,6),(7,8)

so that I can make a for loop for 64 data using PHP.

  • 写回答

2条回答 默认 最新

  • dongzhong6675 2013-04-16 04:23
    关注
    <?php
    $a = 'val1,val2,val3,val4';
    
    function x($value)
        {
            $buffer = explode(',', $value);
            $result = array();
    
            while(count($buffer))
                { $result[] = array(array_shift($buffer), array_shift($buffer)); }
    
            return $result;
        }
    
    $result = x($a);
    
    var_dump($result);
    ?>
    

    Shows:

    array(2) { [0]=> array(2) { [0]=> string(4) "val1" [1]=> string(4) "val2" } [1]=> array(2) { [0]=> string(4) "val3" [1]=> string(4) "val4" } }
    

    If modify it, then it might help you this way:

    <?php
    $a = '1,2,3,4';
    
    function x($value)
        {
            $buffer = explode(',', $value);
            $result = array();
    
            while(count($buffer))
                { $result[] = sprintf('(%d,%d)', array_shift($buffer), array_shift($buffer)); }
    
            return implode(',', $result);
        }
    
    $result = x($a);
    
    var_dump($result);
    ?>
    

    Which shows:

    string(11) "(1,2),(3,4)"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大