[英]How to get values separated with comma from database using PHP
I have four files named comma separated in one field in database like this file1
, file2
, file3
, file4
. 我有四个名为逗号的文件,它们分隔在数据库的一个字段中,例如file1
, file2
, file3
, file4
。 It may change depending on files uploading. 视上传的文件而定。 User can upload maximum 4 files, minimum one file. 用户最多可以上传4个文件,最少可以上传一个文件。 But I was not able to get it. 但是我没办法。 I used explode but it's taking too long. 我使用了explode,但是花费的时间太长。
I am using this code: 我正在使用此代码:
$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);
}
I am getting output like this : 我得到这样的输出:
[{"imagearray":["http:\/\/projects.santabantathegreat.com\/glassicam\/uploads\/60\/30\/file1.jpg","http:\/\/projects.santabantathegreat.com\/glassicam\/uploads\/60\/30\/file2.jpg",""]}]
If you see this imageArray last one is getting "" that means some in file1
, file2
, file3
, file4
one name is missing so I want to show if any filename is not there means I don't want to show null values with "" 如果您看到此imageArray的最后一个正在获取“”,则表示file1
, file2
, file3
, file4
某个名称丢失,因此我想显示是否没有文件名,这意味着我不想使用“”显示空值
i have a field with file1,file2,file3,file4 so times we will have file1,file3 then remaining will not there so i want to count file name separated with commas and if file1 is there is should print that if file3 is there not then it shouldn't show with "" 我有一个包含file1,file2,file3,file4的字段,因此,我们将拥有file1,file3,然后剩余的将不在那里,所以我想计算用逗号分隔的文件名,如果file1存在,则应打印,如果file3没有它不应显示为“”
You could have used split()
, but its deprecated in PHP 5.3.0 . 您本可以使用split()
,但在PHP 5.3.0中已弃用了它。 So, instead you are left with: 因此,您只剩下:
explode()
which is substantially faster because it doesn't split based on a regular expression, so the string doesn't have to be analyzed by the regex parser. explode()
更快,因为它不会基于正则表达式进行拆分,因此不必由正则表达式分析器分析字符串。 or 要么
preg_split()
which is faster and uses PCRE regular expressions for regex splits. preg_split()
,速度更快,并使用PCRE正则表达式进行正则表达式拆分。 With preg_split()
you could do: 使用preg_split()
您可以执行以下操作:
<?php
$encoded_data = json_encode($data);
$images = preg_split('/,/', $encoded_data->imagearray);
?>
I would say that explode()
is more appropriate for this. 我会说explode()
更适合于此。
<?php
$encoded_data = json_encode($data);
$images = explode(',', $encoded_data->imagearray);
print_r($images);
?>
Resources: What is the difference between split() and explode()? 资源: split()和explode()有什么区别?
You shouldn't have empty values in your array in the first place. 首先,数组中不应有空值。 But if you still have any empty values you could use preg_split()
something like this one here . 但是,如果您仍然有任何空值,则可以在此处使用preg_split()
这样的值。
Similarly you can use array_filter() to handle removal of values (null, false,'',0)
: 同样,您可以使用array_filter()处理值(null, false,'',0)
除去:
print_r(array_filter($images));
There are so many answers here in this forum that do exactly what you are asking: Remove empty array elements , Delete empty value element in array . 这个论坛中有很多答案可以完全满足您的要求: 删除空数组元素 , 删除 数组中的 空值元素 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.