gpt4 book ai didi

php - 按工作日顺序使用工作日键对数组进行排序

转载 作者:可可西里 更新时间:2023-11-01 13:40:04 25 4
gpt4 key购买 nike

我想按星期顺序使用工作日键对数组进行排序,如下所示:星期一、星期二、星期三、星期四、星期五、星期六。

给定这样的输入:

Array
(
[Thursday] => 8
[Friday] => 7
[Monday] => 9
[Tuesday] => 12
[Wednesday] => 8
[Saturday] => 17
)

我想要这样的结果:

Array
(
[Monday] => 9
[Tuesday] => 12
[Wednesday] => 8
[thusday] => 8
[friday] => 7
[Saturday] => 17
)

请帮助。

最佳答案

以下代码没有使用任何排序函数..换句话说..在此上下文中不需要排序。

<?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);

输出:

Array
(
[Monday] => 9
[Tuesday] => 12
[Wednesday] => 8
[Thursday] => 8
[Friday] => 7
[Saturday] => 17
)

关于php - 按工作日顺序使用工作日键对数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21718653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com