简体   繁体   中英

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 . It may change depending on files uploading. User can upload maximum 4 files, minimum one file. But I was not able to get it. I used explode but it's taking too long.

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 ""

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 ""

You could have used split() , but its deprecated in 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.

or

  • preg_split() which is faster and uses PCRE regular expressions for regex splits.

With preg_split() you could do:

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

I would say that explode() is more appropriate for this.

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

Resources: What is the difference between split() and 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 .

Similarly you can use array_filter() to handle removal of values (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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM