帮助我进行 php 数组排序
我有这个数组
$arr = array('test1', 'test', 'test3', 'var1', 'var5', 'var');
,我想将数组修剪/排序为这样
$arr = array('test', 'var');
I have this array
$arr = array('test1', 'test', 'test3', 'var1', 'var5', 'var');
and I want to trim/sort the array to be like this
$arr = array('test', 'var');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这个问题有点模糊;在我看来,您好像在问如何过滤掉不包含数字的元素,而提供的
$arr
只是一个示例。在这种情况下,您可以执行以下操作:如果您的数组发生更改,这也将起作用。如果您需要按字母顺序对结果进行排序,那么:
不过我可能是错的;在这种情况下,这有点矫枉过正了。
This question is a little vague; it sounds to me like you're asking how to filter out the elements that do not contain a digit, and the provided
$arr
is just an example. In which case you can do the following:This will also work if your array changes. If you need to sort the results alphabetically, then:
I could be wrong though; in which case this is slightly overkill.
对 array_splice() 的几次调用即可完成此操作,但它们不能嵌套,因为它需要数组引用,并且不会接受函数调用的输出作为输入参数。
A couple of calls to
array_splice()
will do it, however they cannot be nested because it expects an array reference and will not accept the output of a function call as an input parameter.如果项目位置发生移动,则存在很多变化的可能性,但根据您发布的内容,我认为这是最快的。
There's a lot of possibilities for variability if the item positions ever move around, but based on what you've posted, I think this is quickest.