循环移位关联数组? [英] Circularly shifting associative array?

查看:78
本文介绍了循环移位关联数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中工作,假设您有一个关联数组:

Working in PHP, assume you have an associative array:

'Monday' => 'mon'
'Tuesday' => 'tue'
'Wednesday' => 'wed'
'Thursday' => 'thur'
'Friday' => 'fri'
'Saturday' => 'sat'
'Sunday' => 'sun'

您如何执行圆形"阵列移位?您说要转移一下,以便数组从星期三开始,一直持续到整个7天,直到星期二结束?

How could you perform a "circular" array shift? Say shifting things so that the array starts with Wednesday, and proceeds through all 7 days, ending with Tuesday?

一个重要的注意事项:我需要按键执行此操作,因为我还有其他代码可以确定从哪一天开始上班.

An important note: I need to do this by key, as I have other code determining what day the shift needs to start at.

推荐答案

无需循环

 $arr=array('Monday' => 'mon',
'Tuesday' => 'tue',
'Wednesday' => 'wed',
'Thursday' => 'thur',
'Friday' => 'fri',
'Saturday' => 'sat',
'Sunday' => 'sun');
//say your start is wednesday
$key = array_search("Wednesday",array_keys($arr));
$output1 = array_slice($arr, $key); 
$output2 = array_slice($arr, 0,$key); 
$new=array_merge($output1,$output2);
print_r($new);

这篇关于循环移位关联数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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