[英]alphanumeric sorting with php and preg_match
I have an array called $itemArray[] that contains a set of strings, such as: 我有一个名为$ itemArray []的数组,其中包含一组字符串,例如:
myItem_1,
myItem_2,
myItem_3,
myItem_4,
anotherName_1,
anotherName_2,
anotherName_3,
anotherName_4
I also have the following code that goes through the array and filters out the ones that contain 'myItem'. 我也有以下代码遍历数组,并过滤出包含“ myItem”的代码。 The loop works, however, it returns results in varied order. 循环有效,但是它以不同顺序返回结果。 Is there a way to sort the result of the array so that it lists them alphanumerically? 有没有一种方法可以对数组的结果进行排序,以便按字母数字顺序列出它们?
foreach($itemArray as $item) {
if (preg_match('/myItem/',$item))
echo '$item';
}
I don't have much experience with php and would definitely appreciate the help! 我在php方面没有太多经验,一定会感谢您的帮助! Thank you! 谢谢!
PHP supports lots of ways to sort arrays, see how they work here . PHP支持多种对数组进行排序的方法,请参见此处的工作原理。 You could run it as asort($itemArray)
. 您可以将其作为asort($itemArray)
。
The right way to solve this is by sorting the array prior to outputting it. 解决此问题的正确方法是在输出之前对数组进行排序。 Something along the lines of: 类似于以下内容:
asort($itemArray);
foreach($itemArray as $item) {
if (preg_match('/myItem/',$item))
array[index++] = $item;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.