通过 PHP 的爆炸/内爆删除 CSV 列表中的任何(第一个、中间、最后一个、单个)项目
$pieces = explode(",", $userList);
$key=array_search($deleteuser, $pieces);
if(FALSE !== $key)
{
unset($pieces[$key]);
}
else
return FALSE;
$userList = implode(",", $pieces);
我正在寻找有关如何修改此代码以从 CSV 列表中删除元素的输入。该用户应该存在于该系统中,并且即使系统中只有最后一个用户,它也应该可以正常工作(因此不会存在任何命令)。
$pieces = explode(",", $userList);
$key=array_search($deleteuser, $pieces);
if(FALSE !== $key)
{
unset($pieces[$key]);
}
else
return FALSE;
$userList = implode(",", $pieces);
I'm looking for inputs into how to rework this code to remove an element from a CSV list. The user should exist in this system and it should work fine even if there is one last user in the system (so no commands will exist).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的猜测是您对最后一个/单个项目有问题,因为您正在通过 fgets() 从文件中读取该行,并且没有从字符串中删除尾随换行符。在这种情况下,您确实应该看看 fgetcsv()。
无论如何,要修复您的函数,请将 trim() 应用于输入字符串(或应用于如果您愿意,可以爆炸)以删除空格(包括换行符)。
打印
我使用 array_keys 而不是 array_search() 以防万一用户名可以多次出现名单。
My guess is that you had problems with the last/single item because you're reading the line from a file e.g. via fgets() and you didn't remove the trailing line break from the string. In that case you really should take a look at fgetcsv().
Anyway, to fix your function apply trim() to the input string (or to all array elements after the explode if you like) to remove whitespaces including line breaks.
prints
I've used array_keys instead of array_search() just in case the username can appear more than once in the list.
我不觉得代码有那么糟糕。但你可以使用例如:
但它并不比你的更好......
i dont find the code so bad at all. but you could use for example:
but it isnt better then yours...
给你:
但即使这问了你的问题,你的实现似乎解决了“我需要从 CSV 中删除特定用户”,而不是“我需要删除 CSV 中的最后一项”,实际问题是什么?
Here you go:
But even though this asks your question, your implementation seems to solve "I need to remove a specific user from a CSV", not "I need to remove the last item in a CSV", what is the actual problem?