I have a php code to read multiselect values from database.
the value maybe a single string like 1 or 2 or ... etc .
or maybe a multi as 1|*|3|*|150
.
I want a php code to check value if single , so it remain single.
but if multi as 1|*|3|*|150
so it convert to ["1","3","150"]
How can I do that /
thank you

- 3
- 2
-
So is it `1||3||150` or `1|3|150`, and is it `["1","3","150"]` or `["1","3",150]` – Lawrence Cherone Jan 12 '18 at 22:05
-
sorry I correct the question again – Mahmood Esali Jan 12 '18 at 22:14
-
By single, do you mean `[1]` or `1`. Do you want it as a single value or a single item in an array. Personally, I would use the array all the time. Otherwise you have to do type checking. – ArtisticPhoenix Jan 12 '18 at 22:28
-
Why are you storing it like this in the database? Normalize your data so you have each value in separate row of another table. – Barmar Jan 12 '18 at 22:36
-
Why do you have `*` in the string? Why isn't it just `1|3|150`? – Barmar Jan 12 '18 at 22:36
-
I just wanted to throw this out there, whenever I save a "list" to the Database (instead of making a table of it, typically for tree data) I do it this way, `|1|3|150|` with the delimiters at the front and back. The reason is for searches like this `LIKE '%1%'`, in the first case `1|3|150` un-wrapped, that will match both `1` and the `150` but if you wrap it you can search like this `LIKE '%|1|%'` which will only match the `1`. And it's no harder to do `explode('|', trim($var,'|'))` – ArtisticPhoenix Jan 13 '18 at 05:22
1 Answers
It's just explode
. explode
can use a delimiter consisting of multiple characters.
Your string looks fancier than just comma-separated values, but the delimiter appears to be consistent, so
$array = explode('|*|', $string);
should work just fine.
It seems I misunderstood the question initially, and you don't want an array as the result, you want a sort of string representation of an array. In that case, you can just str_replace
the delimiter instead of using explode
.
$string = '["' . str_replace('|*|', '","', $string) . '"]';
Some unsolicited advice, for you or anyone else who reads this later: In general this is not how I would recommend storing multiple associated values in a database. See this great answer for some compelling reasons not to do this:
Is storing a delimited list in a database column really that bad? (Spoiler alert: Yes.)

- 41,125
- 10
- 61
- 80
-
I would add, `if(false !== strpos($string,'|*|'))` because OP states `I want a php code to check value if single , so it remain single`. Maybe, I ask them in the comments if they really want to do that ... lol – ArtisticPhoenix Jan 12 '18 at 22:27
-
@ArtisticPhoenix maybe so. I think it's kind of hard to tell exactly what the OP wants. Which probably means I shouldn't have answered it, but I thought it looked like they might have just been trying to overcomplicate something simple. Maybe if they clarify what they're trying to do, I can improve the answer, (or delete it if I misunderstood completely). – Don't Panic Jan 12 '18 at 22:35
-
You could just check if `count($array) == 1` then do `$array = array[0]` – Barmar Jan 12 '18 at 22:37
-
thank you , after I explode it to $array ,how I convert this $array to `["1","3",150]` – Mahmood Esali Jan 12 '18 at 22:48
-
@MahmoodEsali after explode, `$array` should contain `["1","3","150"]`. Is 150 supposed to be an int instead of a string? Why does it matter? PHP should be able to convert a numeric string to a number, and will do so automatically in most cases. And if it does matter, what determines which numbers should be converted to ints and which ones should stay as strings? – Don't Panic Jan 12 '18 at 22:52
-
@Don'tPanic `1|*|3|*|150` become an array of integers after this code `$array = explode('|*|', $string);` , now I want to print or convert this array to string as `["1","3","150"]` . How I can do that ? in database the value is `1|*|3|*|150` and I want to upload the value to database again as `["1","3","150"]` thank you – Mahmood Esali Jan 12 '18 at 23:06
-
1@MahmoodEsali I think I misunderstood the question. I edited the answer to show a way to get that string. – Don't Panic Jan 12 '18 at 23:18
-