【问题标题】:Combine Two String with php将两个字符串与php结合起来
【发布时间】:2011-08-02 08:41:55
【问题描述】:

我有两个刺痛

a, b, d, e

1, 52, 34, 56

我想把两个字符串组合成

a/1, b/52, d/34, e/56

我的代码是这样写的:-

  $result2 = mysql_query($query2);
  while($row2=mysql_fetch_array($result2)){
    $arr2 = explode(",",$row2['string1']);
    $arr1 = explode(",",$row2['string2']);
    for($i2 = 0; $i2 < count($arr1); $i2++) {
      $raw_str = "".$arr2[$i2]."/".$arr1[$i2].",";
      $query3 = "UPDATE table SET com_str='".$raw_str."' WHERE id='".$row2['id']."'";
      if (!mysql_query($query3))
      {
          die('Error: ' . mysql_error());
      }
    }
   }

我的代码只能将最后一个值存储到数据库中:-

   e/56,

但我想将完整的字符串存储到数据库中。

感谢提前

【问题讨论】:

    标签: php arrays


    【解决方案1】:
    $letters = 'a, b, d, e';
    $numbers = '1, 52, 34, 56';
    
    preg_match_all('/\w+/', $letters, $letters);
    preg_match_all('/\d+/', $numbers, $numbers);
    
    $final = array();
    foreach ($letters[0] AS $key => $letter)
        $final[] = $letter . '/' . $numbers[0][$key];
    
    echo join(', ', $final);
    // a/1, b/52, d/34, e/56
    

    【讨论】:

      【解决方案2】:

      试试这个代码:

      $result2 = mysql_query($query2);
      while($row2=mysql_fetch_array($result2)){
          $arr2 = explode(",",$row2['string1']);
          $arr1 = explode(",",$row2['string2']);
          $raw_str = array();
          for($i2 = 0; $i2 < count($arr1); $i2++)
              $raw_str[] = $arr2[$i2].'/'.$arr1[$i2];
          $query3 = "UPDATE table SET com_str='".implode(',',$raw_str)."' WHERE id='".$row2['id']."'";
          if (!mysql_query($query3)){
              die('Error: ' . mysql_error());
          }
      
      }
      

      您还应该查看naming your variables

      【讨论】:

        【解决方案3】:

        试试这个:

          $result2 = mysql_query($query2);
          while($row2=mysql_fetch_array($result2)){
            $arr2 = explode(",",$row2['string1']);
            $arr1 = explode(",",$row2['string2']);
            $raw_str = "";
            for($i2 = 0; $i2 < count($arr1); $i2++) {
              $raw_str .= "".$arr2[$i2]."/".$arr1[$i2].",";
            }
            $query3 = "UPDATE table SET com_str='".$raw_str."' WHERE id='".$row2['id']."'";
            if (!mysql_query($query3))
            {
                die('Error: ' . mysql_error());
            }
        
           }
        

        【讨论】:

        • 这将在最后添加一个额外的 , 。检查我的答案以进行修复。
        【解决方案4】:
        $str1 = explode(',', 'a, b, d, e');
        $str2 = explode(',', '1, 52, 34, 56');
        
        if (count($str1) == count($str2))
        {
            $result = array();
        
            foreach ($str1 as $key => $value)
            {
                $result[] = trim($value) . '/' . trim($str2[$key]);
            }
        
            $result = implode(', ', $result);
        }
        
        var_dump($result);
        

        【讨论】:

          猜你喜欢
          • 2021-09-28
          • 1970-01-01
          • 2018-09-08
          • 1970-01-01
          • 2023-04-06
          • 2020-07-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多