English 中文(简体)
通过PHP的探索/最终处理
原标题:Removing any (first, middle, last, single) item in a CSV list via PHP s explode / implode
$pieces = explode(",", $userList);

$key=array_search($deleteuser, $pieces);
if(FALSE !== $key)
{
    unset($pieces[$key]);
}
else
    return FALSE;

$userList = implode(",", $pieces);

I m 寻求对如何重新制定这一法典的投入,以便从加拿大社会保险协会名单上删除一个内容。 用户应当存在于这一系统中,即使系统有一个最后用户,也应当进行罚款(因此不存在指挥)。

最佳回答

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.

<?php
echo  -- , foo( thisone ,  a,bcd,thisone,e ), "--
";
echo  -- , foo( thisone ,  thisone,e ), "--
";
echo  -- , foo( thisone , "e, thisone
"), "--
";
echo  -- , foo( thisone ,  thisone ), "--
";
echo  -- , foo( thisone ,   ), "--
";
echo  -- , foo( thisone ,  a,thisone,b,thisone,c,thisone ), "--
";

function foo($deleteuser, $userList) {
  $pieces = array_map( trim , explode( , , $userList));

  foreach( array_keys($pieces, $deleteuser) as $key ) {
    unset($pieces[$key]);
  }
  return  implode( , , $pieces);
}

印刷

--a,bcd,e--
--e--
--e--
----
----
--a,b,c--

I ve used array_keys,而不是阵列_search() ,就在用户名称可以不止一次出现在清单中。

问题回答

i 故意发现该法典完全坏。 但你可以使用:

$newUserList = str_replace(",,",",",str_replace($deleteuser,  ,$userList));

但它比你好。

各位:

$arr = explode( , , $userList);
array_pop($arr);
$userList = implode( , , $arr);

但是,尽管这问你的问题,但你的执行似乎解决了“我需要把某个特定用户从特别志愿人员中删除”而不是“我需要在特别志愿人员中删除最后一个项目”,实际问题是什么?





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签