在逗号分隔的字符串中切换值的位置

I have a comma delimited string held within a database field that could contain any number of values:

23,45,21,40,67,22

I need to be able to somehow switch two values, so for example I know I need to move 45 one position down the string, so I end up with:

23,21,45,40,67,22

The reason for this is that the numbers all correspond to the IDs held in another database table, and their position in the sting determine the order those items will be printed on screen. Before you ask about database design - I've inherited it and it cannot be changed without significant work to an entire application.

So I've thought about exploding the string, identifying the position of the target number and swapping it with the one next-door, but I'm unsure of how this can be achieved when the total number of values is not known.

Any things? I suspect the solution will be cumbersome, but needs must!!

查看全部
dongyuli4538
dongyuli4538
2011/02/10 20:36
  • string
  • php
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

3个回复