基于两个不同的数组索引匹配输出

时间:2017-09-27 21:00:12

标签: php

我遇到的情况是,一个db表(t1)中的结果与一周的日期一起存储,对应于这个数组:

$xweek = array(1 => "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

但是,我需要从另一个(t2)表中输出数据,其中星期几基于此数组:

$yweek = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");

正如您所看到的,索引不匹配。我该如何同步?我是否需要创建另一个可以在t1中使用的数组?

$zweek = array(6 => 1, 0 => 2, 1 => 3, 2 => 4, 3 => 5, 4 => 6, 5 => 7);

foreach ($yweek AS $k => $v) {

    $t1_Data[$k];
}

2 个答案:

答案 0 :(得分:0)

您可以结合array_shift() / array_unshift()array_push()完成此操作。

$xweek = array(1 => "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",     "Friday", "Saturday");
$yweek = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday",          "Saturday", "Sunday");

如果您想转换$xweek以匹配$yweek的格式:     array_push($ xweek,array_shift($ xweek));

var_dump($xweek);

如果您想转换$yweek以匹配$xweek的格式:     array_push($ yweek,array_shift($ yweek));

var_dump($yweek);

答案 1 :(得分:0)

您可以使用将美国星期日订单转换为欧洲人的功能:

function getEuroDow($d){
    return $d==0 ? 6 : $d-1;
}

然后使用它:

foreach($t1_Data AS $k => $v) {
    $t1_Data[getEuroDow($k)];
}

这样你就不必改变阵列了。