比较多个数组值并返回重复项的键

时间:2017-12-29 09:35:49

标签: php arrays

$hours = array(
    'Mo' => array('open' => true, 'from' => '10:00', 'to' => '20:00'),
    'Tu' => array('open' => true, 'from' => '10:00', 'to' => '18:00'),
    'We' => array('open' => true, 'from' => '10:00', 'to' => '20:00'),
    'Th' => array('open' => true, 'from' => '10:00', 'to' => '18:00'),
    'Fr' => array('open' => true, 'from' => '10:00', 'to' => '18:00'),
    'Sa' => array('open' => true, 'from' => '11:00', 'to' => '16:00'),
    'Su' => array('false' => true, 'from' => '11:00', 'to' => '16:00')
);

$res = [];
foreach($hours as $k=>$v) {
    if($v['open']){
        $t = $v['from'] . ' - ' . $v['to'];
        if (! isset($res[$t]))  $ret[$t] = [];
        $res[$t][] = $k;
    }
}
$final = [];
foreach($res as $k=>$v) {
  $final[] = implode(', ', $v) . ' ' . $k;
}
echo implode(' ', $final);

已修改为包含评论

尝试找到一个解决方案来比较多维数组中的多个值并返回组中的匹配数组键。

示例数组:

$hours = array(
'Mo' => array('open' => true, 'from' => '10:00', 'to' => '20:00'),
'Tu' => array('open' => true, 'from' => '10:00', 'to' => '18:00'),
'We' => array('open' => true, 'from' => '10:00', 'to' => '20:00'),
'Th' => array('open' => true, 'from' => '10:00', 'to' => '18:00'),
'Fr' => array('open' => true, 'from' => '10:00', 'to' => '18:00'),
'Sa' => array('open' => true, 'from' => '11:00', 'to' => '18:00'),
'Su' => array('open' => true, 'from' => '11:00', 'to' => '18:00')
);

因此,上面的例子会导致 Mo&我们组合在一起, Tu,Th,Fr 组合在一起, Sa&苏组合在一起。 我的目的是使用结果来缩小ld + jason模式的小时数。期望的最终结果将是一个如下所示的字符串:Mo, We 10:00 - 20:00 Tu, Th, Fr 10:00 - 18:00 Sa, Su 11:00 - 16:00实现此目的的最佳方法是什么?

我看着数组相交,但这似乎不是我想要的。 (或者至少我无法解决如何从我共享的多维数组中获取我需要使用它的结果。这是比较两个数组而不是多维数组的多个级别。

3 个答案:

答案 0 :(得分:0)

您应该使用 array_intersect 功能。

if (Input.GetKeyDown(KeyCode.Q) && attTraking == 0)
{
    anim.SetTrigger(attackOneToHash);
    attTraking = 1;
}

else if (Input.GetKeyDown(KeyCode.Q) && attTraking == 1)
{
    anim.SetTrigger(attackTwoToHash);
    attTraking = 2;

}

else if (Input.GetKeyDown(KeyCode.Q) && attTraking == 2)
{
    anim.SetTrigger(attackThreeToHash);
    attTraking = 0;
}

输出

.sqlproj

答案 1 :(得分:0)

我不知道是否有更好的解决方案,但这可以给你一个好的结果。

$hours = array(
    'Mo' => array('open' => true, 'from' => '10:00', 'to' => '08:00'),
    'Tu' => array('open' => true, 'from' => '10:00', 'to' => '06:00'),
    'We' => array('open' => true, 'from' => '10:00', 'to' => '08:00'),
    'Th' => array('open' => true, 'from' => '10:00', 'to' => '06:00'),
    'Fr' => array('open' => true, 'from' => '10:00', 'to' => '06:00'),
    'Sa' => array('open' => true, 'from' => '11:00', 'to' => '04:00'),
    'Su' => array('open' => true, 'from' => '11:00', 'to' => '04:00')
);

$newHours = [];
foreach ($hours as $day => $hour){
    $index = array_search($hour, $newHours);
    if ( $index == false ){
        $newHours[$day] = $hour;
    }else{
        unset($newHours[$index]);
        $newHours[$index . ',' . $day] = $hour; 
    }
}

print_r($newHours);

结果将是:

Array
(
    [Mo,We] => Array
        (
            [open] => 1
            [from] => 10:00
            [to] => 08:00
        )

    [Tu,Th,Fr] => Array
        (
            [open] => 1
            [from] => 10:00
            [to] => 06:00
        )

    [Sa,Su] => Array
        (
            [open] => 1
            [from] => 11:00
            [to] => 04:00
        )

)

答案 2 :(得分:0)

Flipp阵列并收集具有相同时间表的日子

$res = [];
foreach($hours as $k=>$v) {
  $t = $v['from'] . ' - ' . $v['to'];
  if (! isset($res[$t]))  $ret[$t] = [];
  $res[$t][] = $k;
}

foreach($res as $k=>$v) {
  echo implode(', ', $v) . ' ' . $k ."\n";
}

demo