php implode pop 可以做到吗?

发布于 2024-08-02 06:53:27 字数 303 浏览 5 评论 0原文

例如:从数据库字段检索现有数据:a,b,c,d,e 所以现在

$data= "a,b,c,d,e";

例如:要添加的新数据--- cc, gg 保存到数据库中的最终值将被

$finaldatatobeupdatedintodb= "a,b,c,d,e,cc,gg";

检索并添加更多值。

然后将其追加回逗号列表中。

如何纯粹通过内爆和爆炸来做到这一点,而不需要将其放入数组中进行 for 循环来重建/重新添加添加新值的东西?

eg: existing data retrieve from db field: a,b,c,d,e
so now

$data= "a,b,c,d,e";

eg: new data to be added--- cc, gg
final value saved into db will be

$finaldatatobeupdatedintodb= "a,b,c,d,e,cc,gg";

Retrieve out and add in few more values.

Then append it back into the comma list.

How to do it purely with implode and explode, without needing to put it into array to for loop to reconstruct/re-add the thing with new values added?

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

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

发布评论

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

评论(5

沩ん囻菔务 2024-08-09 06:53:27

如果您确实想使用 explodeimplode,你可以做这样的事情:

首先,分解你拥有的字符串:

$data= "a,b,c,d,e";
$list = explode(',', $data);
var_dump($list);

这会给你:

array
  0 => string 'a' (length=1)
  1 => string 'b' (length=1)
  2 => string 'c' (length=1)
  3 => string 'd' (length=1)
  4 => string 'e' (length=1)

然后,添加新元素:

$to_add = array('cc', 'gg');
$new_list = array_merge($list, $to_add);
var_dump($new_list);

$new_list现在是:

array
  0 => string 'a' (length=1)
  1 => string 'b' (length=1)
  2 => string 'c' (length=1)
  3 => string 'd' (length=1)
  4 => string 'e' (length=1)
  5 => string 'cc' (length=2)
  6 => string 'gg' (length=2)

最后,使用 ',' 作为分隔符来内爆 $new_list :

$output = implode(',', $new_list);
var_dump($output);

你会得到 :

string 'a,b,c,d,e,cc,gg' (length=15)

当然,如果你从一个数组开始,那就少了一次爆炸;如果您要添加的数据不是数组,则需要再进行一次爆炸...

但是,正如 Rob 指出的,在简单的情况下,你正在呈现,不需要这么复杂的代码:字符串连接就足够了;-)

基于数组/爆炸/内爆的解决方案的优点是,您可以在将最终数组内爆之前对其进行处理一个字符串(例如,您可以对其进行排序)

If you really want to use explode and implode, you could do something like this :

First, explode the string you have :

$data= "a,b,c,d,e";
$list = explode(',', $data);
var_dump($list);

Which will give you :

array
  0 => string 'a' (length=1)
  1 => string 'b' (length=1)
  2 => string 'c' (length=1)
  3 => string 'd' (length=1)
  4 => string 'e' (length=1)

Then, add the new elements :

$to_add = array('cc', 'gg');
$new_list = array_merge($list, $to_add);
var_dump($new_list);

$new_list is now :

array
  0 => string 'a' (length=1)
  1 => string 'b' (length=1)
  2 => string 'c' (length=1)
  3 => string 'd' (length=1)
  4 => string 'e' (length=1)
  5 => string 'cc' (length=2)
  6 => string 'gg' (length=2)

And, finally, implode the $new_list, using ',' as a separator :

$output = implode(',', $new_list);
var_dump($output);

And you get :

string 'a,b,c,d,e,cc,gg' (length=15)

Of course, if you start with an array, that's one less explode to do ; and if the data you want to add is not an array, that's one more explode to do...

But, as Rob pointed out, in the simple case you are presenting, there is no need for such a complicated piece of code : strings concatenations will be more than enough ;-)

The advantage with the array/explode/implode-based solution is that you can work on the final array before imploding it into a string (say, for instance, you can sort it)

陌伤浅笑 2024-08-09 06:53:27
$finaldatatobeupdatedintodb = $data . ',cc,gg';

这是你所需要的吗?很难准确理解你的问题在问什么。

$finaldatatobeupdatedintodb = $data . ',cc,gg';

Is that what you need? It's difficult to understand exactly what your question is asking.

归属感 2024-08-09 06:53:27

与 Rob 的答案类似,但可能更稳健一些:

$final = $data . (strlen($data) ? "," : "") . "cc,gg";

或者,如果您的额外值以数组形式出现:

$newValues = array("cc", "gg");
$final = $data . (strlen($data) ? "," : "") . implode(",", $newValues);

Similar to Rob's answer, but probably a bit more robust:

$final = $data . (strlen($data) ? "," : "") . "cc,gg";

or, if your extra values are coming in as an array:

$newValues = array("cc", "gg");
$final = $data . (strlen($data) ? "," : "") . implode(",", $newValues);
满天都是小星星 2024-08-09 06:53:27
<?

function addvalues($data, $value) {

  $array = Array();
  $array = explode(",", $data);

  // if value are an array, merge it, or just add as element
  if(is_array($value))
    $array = array_merge($array, $value);
  else
    $array[] = $value;

  return implode(",", $array);
}

// original string
$data = "a,b,c,d,e";

echo "Old data = ".$data."<br>";

// see the function, it will work with both arrays and variables
$data = addvalues($data, Array("cc"));
$data = addvalues($data, "gg");

echo "New data = ".$data."<br>";

?>
<?

function addvalues($data, $value) {

  $array = Array();
  $array = explode(",", $data);

  // if value are an array, merge it, or just add as element
  if(is_array($value))
    $array = array_merge($array, $value);
  else
    $array[] = $value;

  return implode(",", $array);
}

// original string
$data = "a,b,c,d,e";

echo "Old data = ".$data."<br>";

// see the function, it will work with both arrays and variables
$data = addvalues($data, Array("cc"));
$data = addvalues($data, "gg");

echo "New data = ".$data."<br>";

?>
蓝礼 2024-08-09 06:53:27

您能否详细说明为什么需要在数据库中存储逗号分隔值?通常,您会为不同的离散值使用单独的字段。如果您有固定数量的值,则可以使用 SET 字段类型。

通常最好的解决方案是使用单独的表。维基百科关于数据库规范化的历史可能会有所帮助。

Can you give more details about why you need to store comma-separated values in a database? Normally you would use separate fields for different discrete values. If you have a fixed number of values you could instead use the SET field type.

Normally the best solution is to use a separate table. Wikipedia's age on Database normalization may help.

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