1

Hi guys I have an input field in my blade which is an array.

            <div class="form-group">
                <div class="input_fields_wrap">
                    <label for="dependents">Dependents</label>
                    <button class="add_field_button btn btn-sm">+</button>
                    <input type="text" name="dependents[]" class="form-control input-sm">
                </div>
            </div>

I want to save this as a long string separated by a comma every string. And ill save this in my database as dependents. Is it possible?

gwapo
  • 178
  • 2
  • 4
  • 28
  • Any string can be saved in a database. – u_mulder Jan 18 '18 at 06:51
  • but i mean is that to save it as an array how can i make it as one string @u_mulder – gwapo Jan 18 '18 at 06:52
  • What do you mean by `And ill save this in my database as dependents` ? – Niklesh Raut Jan 18 '18 at 06:54
  • If "dependents" are actual records in a table, you're almost certainly better off creating a pivot table to maintain referential integrity. You'll avoid many headaches in the future when someone changes the code and forgets that there are arbitrary relationships stuffed into a string field. – Cy Rossignol Jan 18 '18 at 07:12
  • But I'm planning to save it as a long string to be able to save it in one field – gwapo Jan 18 '18 at 08:15

3 Answers3

2

Yes, possible. Use implode() when inserting to database.

$dependents = implode(',', $request->input('dependents'));

And use explode() when retrieving from database to array.

$dependents[] = explode(',', $db_items->dependents);
Sohel0415
  • 9,523
  • 21
  • 30
1

Yes you can do it with the help of implode

$data = implode(',',$dataArray);

But I would like to recommend you to use serialize()

$data = serialize($dataArray);

and to get actual array you can use

unserialize($data); // $data should be serialize 

You get fewer efforts when you're using associated/multidimensional array with key

Serialize or Implode

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
  • Whats the difference of using implode and unserialize? – gwapo Jan 18 '18 at 06:56
  • As much as i concerned, serialize won't concatenate array values as a comma separated value, right?? – Sohel0415 Jan 18 '18 at 07:01
  • @Sohel0415 exactly. It saves entire array as serialize with the respective key on each element. Once we unserialize it will restore the array way it been – Abdulla Nilam Jan 18 '18 at 07:19
  • php serialize is probably the worst way to save it, unless you save an instance of a class. Data saved this way is impossible to edit outside of PHP without destroying it. And even more impossible to search on. Even JSON is preferable to that. – ArtisticPhoenix Jan 18 '18 at 07:21
  • @ArtisticPhoenix when you save multi-dimensional array then it might usefull as i mentioned – Abdulla Nilam Jan 18 '18 at 07:22
  • I would use JSON in that case, because at least you could search using `LIKE '%field:value%'` serialize will be like `{s3:foo}` or some crap. where `s` means string and `3` is the length making it near impossible to search, as I said. And if you edit `foo` and make it `food` outside of PHP you will never be able to un-serialize the data, Whereas Json has no issue with such an edit. – ArtisticPhoenix Jan 18 '18 at 07:24
  • None sir I just decided to make a textbox instead of arrayed input fields – gwapo Jan 23 '18 at 02:42
1

As mentioned you can implode/explode it.

Generally speaking saving data this way renders it almost impossible to search on. It's usually better to save it in a separate table as it's essentially a Many To One relationship. That is up to you to decide, but I wouldn't do it just to "cut corners" as things like counting and grouping will suffer because indexing such a field offers much less performance benefit then a separate table.

That said, one suggestion I have is to add the deleimters to the front and back like this:

,1,2,12,13,145,

Why you ask. Because consider this query

SELECT * FROM table WHERE field LIKE '%1%'

Now if that list is field it will match any row that has a 1 anywhere. This is probably not what you want. Such as matching 23,215,54. So the simple solution is to add the delimiters to the front and back and the query as well like this.

SELECT * FROM table WHERE field LIKE '%,1,%'

Now we are only matching fields with ,1,, because we can include the delimiters in the search. It should be pretty obvious that ,1, is not the same as 215.

You have to think hard how you will use this data as you will be limited to searching and grouping using only queries that use Like %data% to match with.

You can simply add them by doing this:

 $var = ','.implode(',', $arr).',';

The good thing is there are easy ways to remove these extra delimiters, if you choose to use them:

$var = trim($var,',');
//then we would explode it 
$arr = explode(',', $var);

You can even do it right in the SQL with

SELECT TRIM(BOTH ',' FROM field ) AS trimmed;

I would be vary careful in your choice of delimiters, if it's possible to have a , in the data at all chose something like | and make sure to strip the delimiter from input data otherwise you may never be able to separate the data properly in the future.

I typically use this method when I use tree data with a Materialized Path schema, but that's a story for another time...

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38