schema.org的动态营业时间

时间:2013-04-12 11:47:31

标签: php schema.org

我是这个论坛的新手,想要求帮助。最近我必须在一组网站中实现schema.org/LocalBusiness微数据,但问题是我需要动态添加它。在开放时间到来之前,一切都很容易。 因此架构希望显示小时数的方式是:

  <meta itemprop="openingHours" content="Mo-Sa 11:00-14:30">Mon-Sat 11am - 2:30pm
  <meta itemprop="openingHours" content="Mo-Th 17:00-21:30">Mon-Thu 5pm - 9:30pm
  <meta itemprop="openingHours" content="Fr-Sa 17:00-22:00">Fri-Sat 5pm - 10:00pm

我设法只分别显示每天的开放时间,但我需要将它们显示为上面的代码。

我尝试这样做是为了从开放时间表格中获取数据到数组。例如:

$days = array('mo' => '13:00-20:00',
        'tu' => '13:00-20:00',
        'we' => '13:00-20:00',
        'th' => '11:00-18:00',
        'fr' => '11:00-18:00',
        'sa' => '15:00-22:00',
        'su' => '13:00-20:00',);

我需要从这个数组中显示的结果是:

<meta itemprop="openingHours" content="Mo-We 13:00-20:00">Mon-We 13:00-20:00
  <meta itemprop="openingHours" content="Th-Fr 11:00-18:00">Th-Fr 11:00-18:00
  <meta itemprop="openingHours" content="Sa 15:00-22:00">Sat 15:00-22:00
  <meta itemprop="openingHours" content="Su 13:00-20:00">Sun 13:00-20:00

并尝试使用许多ifs循环此数组。所以,大概你们大多数人都可以想到这样的事情,我不会做任何事情。这是我的代码。我没有测试过这段代码,因为我无法弄清楚如何检索$ last_key's_value_equal。希望任何人都能理解我,因为我的解释不是很好,但如果有人可以提供帮助,我将不止感激。

foreach($day as $key=>$value){
    if($value == next($day)){
        if($value == next($day)){
            if($value == next($day)){
                if($value == next($day)){
                    if($value == next($day)){
                        if($value == next($day)){
                        $opening_hours .= $key.'-'.$last_key's_value_equal.' '.$value;
                        }else{
                        $opening_hours .= $key.'-'.$last_key's_value_equal.' '.$value;
                        }
                    }else{
                    $opening_hours .= $key.'-'.$last_key's_value_equal.' '.$value;
                    }
                $opening_hours .= $key.'-'.$last_key's_value_equal.' '.$value;
                }else{
                $opening_hours .= $key.'-'.$last_key's_value_equal.' '.$value;
                }
            $opening_hours .= $key.'-'.$last_key's_value_equal.' '.$value;
            }else{
            $opening_hours .= $key.'-'.$last_key's_value_equal.' '.$value;
            }
        $opening_hours .= $key.'-'.$last_key's_value_equal.' '.$value;
        }else{
        $opening_hours .= $key.'-'.$last_key's_value_equal.' '.$value;
        }
    $opening_hours .= $key.'-'.$last_key's_value_equal.' '.$value;
    else{
    $opening_hours .= $key.'-'.$last_key's_value_equal.' '.$value;
    }
}

1 个答案:

答案 0 :(得分:2)

保留一组连续的组。当您遍历$days时,请检查最后一项是否具有相同的小时数。如果是,请将该日添加到同一组;如果没有,请创建一个新组。

,例如,此函数应用于$days数组时:

function group_contiguous_values($arr) {
    $groups = array();
    foreach ($arr as $k => $v) {
        $lastidx = count($groups)-1;
        if (isset($groups[$lastidx][$v])) {
            $groups[$lastidx][$v][] = $k;
        } else {
            $groups[] = array($v => array($k));
        }
    }
    return $groups;
}

会产生这样的结果:

array(
  array('13:00-20:00' => array('mo', 'tu', 'we')),
  array('11:00-18:00' => array('th', 'fr')),
  array('15:00-22:00' => array('sa')),
  array('13:00-20:00' => array('su')),
);

从那里获得你想要的人类可读版本只是肘部油脂的问题。此代码将处理itemprop

$time_templ = '<time itemprop="openingHours" datetime="%s %s">%s %s</time>'."\n";

foreach ($business_hours as $group) {
    list($hours, $days) = each($group);
    list($open, $close) = explode('-', $hours, 2);
    $firstday = ucfirst($days[0]);
    $lastday = (count($days)>1) ? ucfirst(end($days)) : null;
    $meta_days = $firstday;
    if ($lastday) {
        $meta_days .= "-$lastday";
    }
    $meta_hours = $hours;

    printf($time_templ, $meta_days, $meta_hours, $meta_days, $meta_hours);
}

打印哪些:

<time itemprop="openingHours" datetime="Mo-We 13:00-20:00">Mo-We 13:00-20:00</time>
<time itemprop="openingHours" datetime="Th-Fr 11:00-18:00">Th-Fr 11:00-18:00</time>
<time itemprop="openingHours" datetime="Sa 15:00-22:00">Sa 15:00-22:00</time>
<time itemprop="openingHours" datetime="Su 13:00-20:00">Su 13:00-20:00</time>
相关问题