Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中对关联数组排序,然后找到结果的字符串索引_Php_Sorting_Associative Array - Fatal编程技术网

如何在PHP中对关联数组排序,然后找到结果的字符串索引

如何在PHP中对关联数组排序,然后找到结果的字符串索引,php,sorting,associative-array,Php,Sorting,Associative Array,如果我有这样一个关联数组 $array = array(); $array['e01'] = '03/16/2012'; $array['e02'] = '03/14/2012'; $array['e05'] = '03/01/2014'; 我想按日期对数组排序,然后循环遍历结果以获得索引的值,即e01、e02、e05。我需要该索引来从对象检索信息。从。这将按值对其进行排序,并允许您访问每个索引的键和值: $fruits = array("d" => "lemon", "a" =>

如果我有这样一个关联数组

$array = array();
$array['e01'] = '03/16/2012';
$array['e02'] = '03/14/2012';
$array['e05'] = '03/01/2014';
我想按日期对数组排序,然后循环遍历结果以获得索引的值,即e01、e02、e05。我需要该索引来从对象检索信息。

从。这将按值对其进行排序,并允许您访问每个索引的键和值:

$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
使用该函数。

尝试
uasort()
对数组进行排序:

function mysort($a, $b) {
    return (strtotime($a) < strtotime($b)) ? -1 : 1;
}

uasort($array, 'mysort');
函数mysort($a,$b){
返回(strotime($a)

为了获取键,在对关联数组进行排序时,只需使用
array\u keys()

,键和值的顺序会改变,但键与值的关联不会改变。因此,如果键
e01
的值为
03/16/2012
,即使在
uasort
之后(这可能是您正在寻找的,请参阅CrashPeeder的答案)
e01
仍将具有值
03/16/2012
。如果要在保持键顺序的同时更改值的顺序,请执行以下操作:

  • 使用数组_键获取键的当前顺序
  • 使用usort(不需要uasort,因为我们不保留键)按日期对值进行排序
  • 使用array_combine创建一个包含键和值的新数组

对于他使用的日期格式来说,这不正常。它是按字符串排序的。将日期排序为字符串大多数情况下会导致错误的结果。很好,效果非常好。感谢您编写了几行代码来完成所有工作。但是,我将添加一个用户排序函数,将字符串更改为时间。@CrashPeder:以非定域格式写入日期/时间进行处理将允许您忽略这些问题,因为非定域格式从常规到特定:
2012/01/10 12:00:01
。年>月>日>小时>分钟>秒>…@JeffD没错,这就是为什么我说“大多数时候”。不幸的是,人们经常以本地化格式而不是可排序格式存储日期。我认为,如果我将您的uasort()建议与Stewart的答案结合起来,我将有正确的解决方案。他的代码做了我想做的,实际上是排序。但我宁愿添加strotime函数。谢谢你指出这一点。