PHP 5.3或更低版本中的数组不区分大小写的键排序 [英] Array case-insensitive key sort in PHP version 5.3 or less

查看:160
本文介绍了PHP 5.3或更低版本中的数组不区分大小写的键排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP 5.4引入了有用的SORT_FLAG_CASE,使任何其他搜索都不区分大小写.不幸的是,这在PHP 5.3或更低版本中不可用,因此我想知道以下数组如何:

PHP 5.4 introduces the useful SORT_FLAG_CASE for making any other search case insensitive. Unfortunately this isn't available in PHP 5.3 or less and so I was wondering how the following array:

array('a'=>2,'b'=>4,'A'=>1,'B'=>3);

可以分类为:

array('A'=>1,'a'=>2,'B'=>3,'b'=>4);

通常的ksort()函数将其排序为:

As the usual ksort() function sorts it as:

array('A'=>1,'B'=>3,'a'=>2,'b'=>4);

推荐答案

对其中一个PHP函数参考页的评论使我指向了uksort()函数.这(以及uasort()用于按值而不是键进行排序的功能)允许用户编写用于快速排序的比较算法.

A comment on one of the PHP function reference pages pointed me to the uksort() function; this (and the uasort() function for sorting by value instead of key) allow the comparison algorithm for shifting in the quick sort to be written by the user.

使用非常简单的strcasecmp()函数(比较两个字符串并为a> b返回< 0并为a> b返回> 0)将其组合在一起:

Combine this with the very simple strcasecmp() function (which compares two strings and returns <0 for a>b and >0 for a>b) gives you:

uksort($array, 'strcasecmp');

要轻松达到以下效果:

ksort($array,SORT_STRING | SORT_FLAG_CASE);

在PHP 5.3或更低版本中.

In PHP 5.3 or less.

这篇关于PHP 5.3或更低版本中的数组不区分大小写的键排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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