【问题标题】:How to get values separated with comma from database using PHP如何使用 PHP 从数据库中获取用逗号分隔的值
【发布时间】:2015-07-07 09:45:34
【问题描述】:

我在数据库中的一个字段中有四个名为逗号分隔的文件,例如file1,file2,file3,file4。它可能会根据上传的文件而改变。用户最多可以上传 4 个文件,最少 1 个文件。但我无法得到它。我用了explode,但是用的时间太长了。

我正在使用此代码:

      $imagefiles = $row["imagefiles"];


      $cutjobs = explode(",", $imagefiles);
      $cutjobs1 = count($cutjobs);

      $image1 = $cutjobs[0];
      $image2 = $cutjobs[1];
      $image3 = $cutjobs[2];
      $image4 = $cutjobs[3];

      if (empty($image1)) {
        $imagefiles1 = "";
      } else {
        $imagefiles1 = 'http://projects.santabantathegreat.com/glassicam/uploads/'.$registerid.
        "/".$viewjobsid.
        "/".$image1;
      }

      if (empty($image2)) {
        $imagefiles2 = "";
      } else {
        $imagefiles2 = 'http://projects.santabantathegreat.com/glassicam/uploads/'.$registerid.
        "/".$viewjobsid.
        "/".$image2;
      }
      if (empty($image3)) {
        $imagefiles3 = "";
      } else {
        $imagefiles3 = 'http://projects.santabantathegreat.com/glassicam/uploads/'.$registerid.
        "/".$viewjobsid.
        "/".$image3;
      }
      if (empty($image4)) {
        $imagefiles4 = "";
      } else {
        $imagefiles4 = 'http://projects.santabantathegreat.com/glassicam/uploads/'.$registerid.
        "/".$viewjobsid.
        "/".$image4;
      }
    }

    $data[] = array( 'imagearray' => array($imagefiles, $imagefiles1, $imagefiles2, $imagefiles3));


  }
  echo json_encode($data);
}  

我得到这样的输出:

[{"imagearray":["http:\/\/projects.santabantathegreat.com\/glassicam\/uploads\/60\/30\/file1.jpg","http:\/\/projects.santabantathegreat.com\/glassicam\/uploads\/60\/30\/file2.jpg",""]}]

如果您看到此 imageArray 最后一个正在获取“”,这意味着 file1file2file3file4 中的一些名称丢失了,所以我想显示是否有任何文件名不存在意味着我不想用“”显示空值

我有一个包含 file1、file2、file3、file4 的字段,所以有时我们会有 file1、file3,然后剩下的不会在那里,所以我想计算用逗号分隔的文件名,如果 file1 存在,应该打印如果 file3 是没有那么它不应该显示为“”

【问题讨论】:

  • 你为什么不用explode?​​span>
  • 我是 php 新手,除了爆炸还有什么方法可以使用
  • 尽管它可能在您的控制范围内,也可能不在您的控制范围内,但这是一个糟糕的数据库设计。每当您在单个数据库列中组合多个项目时,您都会遇到拆分它的问题。更好的设计是让表包含文件名并连接回主表。

标签: php database php-5.3 php-5.2


【解决方案1】:

您可以使用split(),但它是deprecated in PHP 5.3.0。所以,你只剩下:

  • explode() 更快,因为它不基于正则表达式进行拆分,因此字符串不必由正则表达式解析器分析。

  • preg_split() 速度更快,并且使用 PCRE 正则表达式进行正则表达式拆分。

使用preg_split(),您可以这样做:

<?php
   $encoded_data = json_encode($data); 
   $images = preg_split('/,/', $encoded_data->imagearray);
?>

我会说explode() 更适合这个。

<?php
   $encoded_data = json_encode($data); 
   $images = explode(',', $encoded_data->imagearray);
   print_r($images);
?>

资源: What is the difference between split() and explode()?


首先,您的数组中不应该有空值。但是,如果您仍然有任何空值,您可以使用 preg_split() 之类的 this one here

同样,您可以使用array_filter() 处理删除值(null, false,'',0)

print_r(array_filter($images));

这个论坛中有很多答案完全符合您的要求:Remove empty array elementsDelete empty value element in array

【讨论】:

  • 你是否理解我在图像数组中的问题你有 $imagefiles,$imagefiles1,$imagefiles2,$imagefiles3 如果任何一个都不存在意味着它显示为 null 或 ""
  • 你不应该首先插入空值。
  • 可能你不明白我的问题我有一个包含 file1,file2,file3,file4 的字段所以有时我们会有 file1,file3 然后剩下的就不在那里了所以我想计算文件名分隔用逗号,如果存在 file1 应该打印如果 file3 不存在那么它不应该用 "" 显示
  • 不,你可以看到我是怎么得到projects.santabantathegreat.com/glassicam/…
  • 我应该在哪里使用它而不是数组我应该使用array_filter
猜你喜欢
  • 2016-11-12
  • 1970-01-01
  • 1970-01-01
  • 2013-06-07
  • 1970-01-01
  • 1970-01-01
  • 2017-09-27
  • 1970-01-01
  • 2014-11-20
相关资源
最近更新 更多