修改的最后一个元件以阵列 [英] Modify the last element in an array

查看:106
本文介绍了修改的最后一个元件以阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I modify the last element in an array?

数组是这样的:

$fields = array("firstName = 'Bob', ",
                "lastName = 'Smith', ",
                "email = 'bob@example.com', ",
                "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.

修改确定,所以我创造了这个函数/阵列,这样我就只有一个MySQL的函数来更新用户。有点像检测功能的变化,而且只传回所需的/更改的字段。我不知道有这种方法带来的问题。我想,既然我已经写在我的老功能mysql的查询不应该有这种方式的问题。 ,它在该文件将不会向公众开放。我将使用为我的作品最好的答案,但我要寻找为什么这是有问题的,我会AP preciate评论/链接,什么是不对的做法。谢谢你。

Edit Ok 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.

推荐答案

这样!

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

这篇关于修改的最后一个元件以阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆