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

发布于 2024-10-16 18:23:09 字数 403 浏览 7 评论 0原文

我有一个逗号分隔的字符串保存在数据库字段中,可以包含任意数量的值:

23,45,21,40,67,22

我需要能够以某种方式切换两个值,所以例如我知道我需要将 45 移动到字符串的一个位置,所以我最终with:

23,21,45,40,67,22

这样做的原因是,这些数字都对应于另一个数据库表中保存的 ID,并且它们在字符串中的位置决定了这些项目在屏幕上打印的顺序。在您询问数据库设计之前 - 我已经继承了它,并且如果不对整个应用程序进行大量工作,就无法更改它。

所以我考虑过分解字符串,识别目标数字的位置并将其与隔壁的数字交换,但我不确定当值的总数未知时如何实现这一点。

有什么东西吗?我怀疑解决方案会很麻烦,但必须!!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自在安然 2024-10-23 18:23:09

假设您只需将所需值在数组中向下移动一个位置:

$values = explode(',', $data_string);

$value_to_move = 45;

$value_count = count($values);
for($i=0;$i<$value_count;$i++)
{
    if($values[$i] == $value_to_move)
    {
        if($i < ($value_count-1))
        {   // if the value to move is NOT at the end of the list already
            $values[$i] = $values[$i+1];
            $values[$i+1] = $value_to_move;
            $i++;
        }
    }
}
$new_data_string = implode(',', $values);

assuming you need to only move the desired value down one position in the array:

$values = explode(',', $data_string);

$value_to_move = 45;

$value_count = count($values);
for($i=0;$i<$value_count;$i++)
{
    if($values[$i] == $value_to_move)
    {
        if($i < ($value_count-1))
        {   // if the value to move is NOT at the end of the list already
            $values[$i] = $values[$i+1];
            $values[$i+1] = $value_to_move;
            $i++;
        }
    }
}
$new_data_string = implode(',', $values);
故笙诉离歌 2024-10-23 18:23:09

我只是将它们拉入一个数组并在那里与它们一起工作。再次以逗号分隔的格式写出字符串,并将其重写到数据库中。

I'd just pull them into an array and work with them there. Write the string out in comma-delimited format again, and rewrite that to the DB.

南巷近海 2024-10-23 18:23:09

假设您确切知道要在该列表中切换哪两个值,那么爆炸是最好的选择:

$array = explode(',', $string)

# find the two values (NOTE: *NO* error handling, what if the values aren't there?)
$index1 = array_search($first_value, $array);
$index2 = array_search($second_value, $array);

# swap them
$temp = $array[$index1];
$array[$index1] = $array[$index2];
$array[$index2] = $temp;

# rebuild the array
$string = implode(',', $array);

Assuming you know exactly which two values to switch in that list, then explode is the best option:

$array = explode(',', $string)

# find the two values (NOTE: *NO* error handling, what if the values aren't there?)
$index1 = array_search($first_value, $array);
$index2 = array_search($second_value, $array);

# swap them
$temp = $array[$index1];
$array[$index1] = $array[$index2];
$array[$index2] = $temp;

# rebuild the array
$string = implode(',', $array);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文