I need a one liner to get from this [1, 2, 3] to ["1", "2", "3"]
I am grabbing ids from users:
and this will give me an array with integers like:
However, I need to make it like this:
so basically strings in an array, or perhaps json like format, right?
The questions is how to do that without a closure or multiple variables or foreach loop etc.
Basically I need a one liner to be able to stick it in default() or other form field related parts, etc.
I have tried
But the result is:
So, it encapsulate the whole array as a string instead of its elements.
Any idea how to elegantly solve this problem?
Can I perhaps cast somehow the pluck('id') into string during the get process? Or is there some trick how to do this easy?
7 Replies
Not sure why it has to be a one liner though. 😀
Also don’t encode it.
Thanks but:
still gives me an array of integers and not an array of strings.
I have tried both dd() and to save it in the database and the result is still:
instead of
For example, I have noticed that the checkbox list method saves selected chckboxes, it's saved nicely as ["1","2","3",...] . How it is doing that?
What are you trying to do, I would expect the checkbox list to handle it all when providing those as the options.
I will probably try raw regex and put strings around each element.
Which PHP regex function would be the best for this job?
I think you’re probably over complicating this. That’s why I asked what your were actually trying to accomplish.
In 1 column in the DB I have [1, 2, 3, 4, ...] and in other I have this ["1", "2", "3", "4", ...] and in other [1, 2, 3, 4, ...] so I always have to convert stuff. That's why I want strings and not integers for everything.
Hmm. To me that points to a bigger underlying issue with your data structure / column casts.