1

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

3
  • Why didn't you use explode ? Commented Apr 28, 2015 at 13:42
  • i am new in php is there is any way to use than explode Commented Apr 28, 2015 at 13:52
  • 1
    Although it may or may not be within your control, this is a poor database design. Whenever you combine multiple items in a single database column you will have problems splitting it. A better design would be to have table that contains the file names and joins back to the main table. Commented Apr 28, 2015 at 14:33

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

did you understand my question in image array you have $imagefiles,$imagefiles1,$imagefiles2,$imagefiles3 if any one is not there means it showing null or ""
You shouldn't insert null values in the first place.
may be you didn't understand my question 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 ""
no you can see how am i am getting projects.santabantathegreat.com/glassicam/…
where i should use that instead of array i should use array_filter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.