php 修改数组中的最后一个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14105037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:38:33  来源:igfitidea点击:

Modify the last element in an array

phparrays

提问by John the Ripper

How do I modify the last element in an array?

如何修改数组中的最后一个元素?

The array looks like this:

该数组如下所示:

$fields = array("firstName = 'Bob', ",
                "lastName = 'Smith', ",
                "email = '[email protected]', ",
                "address = '123 anystreet', ");

The array is generated by a script which creates the values and adds the comma/space at the end of each string. I want to remove that comma/space from only the last element in that array. Keep in mind that the values could in fact contain a comma/space combination so only the last element and the last two characters of the last element need to be removed.

该数组由脚本生成,该脚本创建值并在每个字符串的末尾添加逗号/空格。我只想从该数组中的最后一个元素中删除该逗号/空格。请记住,值实际上可以包含逗号/空格组合,因此只需要删除最后一个元素和最后一个元素的最后两个字符。

I've looked at the end()function but I don't think that is going to help since it just gets the value.

我看过这个end()函数,但我认为这不会有帮助,因为它只是获得了价值。

EditOk so I created this function/array so that I would only have one mysql function to update users. Sort of like a detect changes function and it only passes back the required/changed fields. I didn't realize there were problems associated with this approach. I thought that since I already had the mysql queries written in my old functions there shouldn't be an issue with this way. The file that it's in will not be accessible to the public. I'm going to use the best answer that works for me but I'm going to search for why this is problematic and I would appreciate comments/links as to what is wrong with this approach. Thanks.

编辑好的,所以我创建了这个函数/数组,这样我就只有一个 mysql 函数来更新用户。有点像检测更改功能,它只传回所需/更改的字段。我没有意识到这种方法存在相关问题。我认为由于我已经在旧函数中编写了 mysql 查询,因此这种方式应该没有问题。公众无法访问它所在的文件。我将使用对我有用的最佳答案,但我将寻找为什么这是有问题的,我将不胜感激关于这种方法有什么问题的评论/链接。谢谢。

回答by David Harris

Like this!

像这样!

end($array);
$key = key($array);
reset($array);

回答by Chris Ostmo

There's a shorthand way to do this, but it's easier to follow if it's broken out into pieces:

有一种速记方法可以做到这一点,但如果将其分解成碎片,则更容易理解:

$index = count( $fields ) - 1;
$value = $fields[$index];
$fields[$index] = preg_replace( "/,\ $/", "", $value );

回答by arcanine

To change the value of the last numeric element:

要更改最后一个数字元素的值:

$lastValue = array_pop($fields);
$fields[] = rtrim(', ',$lastValue);

If you are preparing these values for a query I would suggest storing everything without commas in an array then calling implode on that array when needed to prevent trailing comma problems

如果您正在为查询准备这些值,我建议将没有逗号的所有内容存储在数组中,然后在需要时在该数组上调用 implode 以防止尾随逗号问题

回答by Yevgeniy Afanasyev

There are few ways:

有几种方法:

1) For associative arrays, if you don't know the last element key, you better find the last element key first and change its value.

1)对于关联数组,如果不知道最后一个元素的key,最好先找到最后一个元素的key,然后改变它的值。

$array[end((array_keys($array)))] .= ' additional text';

2) if you don't know and don't care about keys, you can cut the last element and create a new one.

2)如果你不知道也不关心keys,你可以剪切最后一个元素并创建一个新元素。

$array[] = array_pop($array).' additional text';

回答by Daniel Tonon

Array pop and push are the easiest way to do it for basic arrays. (I know that isn't technically the question but many people will come here looking for the answer in relation to simple arrays as well).

数组弹出和推送是对基本数组执行此操作的最简单方法。(我知道这在技术上不是问题,但很多人也会来这里寻找与简单数组相关的答案)。

<?php

function update_last(&$array, $value){
    array_pop($array);
    array_push($array, $value);     
}

?>

Then you can use the function like this:

然后你可以像这样使用这个函数:

<?php

$array = [1,2,3];

update_last($array, 4); //$array = [1,2,4];

?>

回答by n?k?

stumbled upon this today. i think the easiest non-pointer-breaking way would be:

今天偶然发现了这个。我认为最简单的非指针破坏方法是:

array_splice($array, -1, 1, strtolower(end(array_values($array))).'blah' );

of course you can drop array_values if you dont have to care for the pointer. but i wonder if this is a good way, since the extract-n-replace-stuff of splice could be more demanding than a pop or sth?

当然,如果您不必关心指针,您可以删除 array_values。但我想知道这是否是一个好方法,因为拼接的提取和替换可能比流行音乐或某事要求更高?

回答by Quentin Skousen

I think PHP's Implodefunction might be a good alternative, instead of generating the commas yourself.

我认为 PHP 的Implode函数可能是一个不错的选择,而不是自己生成逗号。

Barring that, you would have to use something like:

除此之外,您将不得不使用以下内容:

$lastfield = $fields[count($fields)-1];
$lastfield = str_split($lastfield,strlen($lastfield)-2);
$fields[count($fields)-1] = $lastfield;

The first and third lines are included to make the second line easier to read, but this could easily be compounded to one line.

包含第一行和第三行是为了使第二行更易于阅读,但这很容易合并为一行。

回答by I_write_perl_no_one_likes

I think people are looking for this:

我认为人们正在寻找这个:

@array[-1] =~ s/,$//g;

works with newlines too:

也适用于换行符:

@array[-1] =~ s/\s$//g;