数组排序与平日为了平日键 [英] Sort array with weekday keys by weekday order
本文介绍了数组排序与平日为了平日键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要在一周中的顺序排序与平日按键阵列,这样的:周一,周二,周三,周四,周五,周六。
I want to sort array with weekday keys in the order of the week, like this: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday.
这样给定输入:
Array
(
[Thursday] => 8
[Friday] => 7
[Monday] => 9
[Tuesday] => 12
[Wednesday] => 8
[Saturday] => 17
)
我想这样的结果是:
I want a result like this:
Array
(
[Monday] => 9
[Tuesday] => 12
[Wednesday] => 8
[thusday] => 8
[friday] => 7
[Saturday] => 17
)
请帮助。
推荐答案
以下code不使用任何排序功能..换句话说..一种是在这种情况下没有必要的。
The following code does not make use of any sorting functions .. In other words.. a sort is unnecessary in this context.
<?php
//Your actual array...
$arr=Array (
'Thursday' => 8,
'Friday' => 7,
'Monday' => 9,
'Tuesday' => 12,
'Wednesday' => 8,
'Saturday' => 17
);
//This is the template array.. Changing this alters the output
$arr2=array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
//A simple loop that traverses all elements of the template...
foreach($arr2 as $v)
{
//If the value in the template exists as a key in the actual array.. (condition)
if(array_key_exists($v,$arr))
{
$arr4[$v]=$arr[$v]; //The value is assigned to the new array and the key of the actual array is assigned as a value to the new array
}
}
//prints the new array
print_r($arr4);
输出:
OUTPUT :
Array
(
[Monday] => 9
[Tuesday] => 12
[Wednesday] => 8
[Thursday] => 8
[Friday] => 7
[Saturday] => 17
)
这篇关于数组排序与平日为了平日键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文