Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何显示按世纪排序的年份列表_Php - Fatal编程技术网

Php 如何显示按世纪排序的年份列表

Php 如何显示按世纪排序的年份列表,php,Php,我有一系列这样的年头: [1500, 1946, 1923, 1258] class Year { const PLAIN = 1; const ARRAY = 2; private $_years = []; public function append($year, $type) { if($type & self::PLAIN && !is_array($year)) { $this-&g

我有一系列这样的年头:

[1500, 1946, 1923, 1258]
class Year {
    const PLAIN = 1;
    const ARRAY = 2;

    private $_years = [];

    public function append($year, $type) {
        if($type & self::PLAIN && !is_array($year)) {
            $this->_years[] = $year;
            return $this;
        } else if($type & self::ARRAY && is_array($year)) {
            $this->_years = $year;
            return $this;
        } else {
            throw new Exception('Unknown type.');
        }
    }

    public function fetch() {
        return $this->_years;
    }
}

class CenturyConversion {
    public static function parse(Year $years) {
        $nav = [];
        foreach($years->fetch() as $year) {
            foreach(range(0, round(max($years->fetch()), -3), 1000) as $century) {
                if(floor((int)$year/1000) * 1000 == $century) {
                    $nav{$century}[] = (int)$year;
                }
            }
        }
        return $nav;
    }
}
我想按世纪顺序显示它们(也显示相应的世纪):

<ul>
  <li>1200
    <ul>
      <li>1258</li>
    </ul>
  </li>
  <li>1500</li>
  <li>1900
    <ul>
      <li>1923</li>
      <li>1946</li>
    </ul>
  </li>
</ul>
我会用简单的话说:


我有一个cms,用户可以选择一年,可以是1942年或1230年。现在我把这些年放在前端,我想根据上面的html构建一个动态导航,你需要把一年的时间循环到最近的世纪,以便能够匹配哪一年属于哪一个世纪。我添加了一个动态的max century,如果您想从min-max year对数组进行排序,只需添加它即可

请在处查看其详细工作情况,或在处查看其如下工作方式



输出:
[3000]=>数组(3){[0]=>int(3333)[1]=>int(3689)[2]=>int(3000)}


这段代码是非常程序化的,您可以修改它并创建一个类。大概是这样的:

[1500, 1946, 1923, 1258]
class Year {
    const PLAIN = 1;
    const ARRAY = 2;

    private $_years = [];

    public function append($year, $type) {
        if($type & self::PLAIN && !is_array($year)) {
            $this->_years[] = $year;
            return $this;
        } else if($type & self::ARRAY && is_array($year)) {
            $this->_years = $year;
            return $this;
        } else {
            throw new Exception('Unknown type.');
        }
    }

    public function fetch() {
        return $this->_years;
    }
}

class CenturyConversion {
    public static function parse(Year $years) {
        $nav = [];
        foreach($years->fetch() as $year) {
            foreach(range(0, round(max($years->fetch()), -3), 1000) as $century) {
                if(floor((int)$year/1000) * 1000 == $century) {
                    $nav{$century}[] = (int)$year;
                }
            }
        }
        return $nav;
    }
}
它有两种使用方式,一种是数组,另一种是一年。这些方法支持int类型和string类型。以下是手动添加年份的方法:

CenturyConversion::parse((new Year)->append(1992, Year::PLAIN)->append('1365', Year::PLAIN));
以下是如何在数组中添加年份:

CenturyConversion::parse((new Year)->append([1992, 1365, '2010'], Year::ARRAY));
解析代码摘要:

首先,我们获取年份的数字输入,并找到数组中的最大值。然后,我们将最大值四舍五入到最接近的千,以便知道停止检查的位置和开始检查的位置。然后我们循环检查每一年的世纪,我们通过检查最接近世纪的年份,将其四舍五入到最接近的千



查看在上运行的OO样式的代码。

这里有另一种方法。如果在同一个世纪中有超过一年的时间,这种方法也有效

<?php

$yearsArray = [1500, 1946, 1923, 1258];
$centuryHash = [];

$yearsArray = array_unique($yearsArray);
sort($yearsArray);

foreach ($yearsArray as $year) {

    $currentCentury = floor($year/100)*100;

    if(!$centuryHash[$currentCentury]){
        $centuryHash[$currentCentury] = [];
    }

    if($currentCentury != $year){
        $centuryHash[$currentCentury][] = $year;
    }
}

//echo "<pre>" . print_r($centuryHash,1) . "</pre>";

echo "<ul>";

foreach ($centuryHash as $century => $centuryYears) {
    echo "<li>" . $century;

    if($centuryYears){
        echo "<ul>";

        foreach ($centuryYears as $year) {
            echo "<li>$year</li>";
        }

        echo "</ul>";
    }

    echo "</li>";
}

echo "</ul>";
在这里你可以看到结果:


  • 1200
    • 1258
  • 1500
  • 1900
    • 1923
    • 1946
      嗯。。。对不起,我不明白你到底在问什么,即使读了好几遍。你能举个例子吗?@EdvinTenovimas在cms中,你选择一年,1942年或1290年。现在这两个数字是在1900和1200世纪之间。所以我需要首先输出主世纪,其次插入一个对应年份的子ul。明白了吗?我已经读了很多遍了,仍然没有任何意义。您希望输出世纪(即:1200)和嵌套在
      ul
      ?@KDOT中的确切日期(即:1243)。是的。问题是它需要是动态的,因为那些年份可能是3000年范围内的任何年份centuries@rob.m我的错!固定的it@rob.m只需更新这一行:
      $currentCentury=floor($year/100)*100
      @KDOT nice,刚刚用真实数据测试了它,是的,它工作正常。在1760年之前,它是在1800以下,而现在它正确地进入1700。谢谢lot@rob.m不客气;)对这个错误感到抱歉@罗伯,我不太明白这一点。如果你有
      $yearray=[1500]
      ,你只想显示“1500 ul父项”吗?我不知道为什么我一直有错误,这是为了什么?这是为了,这是PHP5.6+中的一个新东西,我想,但它应该在没有
      ..
      的情况下工作。如果无法使用它,请将其删除,因为
      max()
      支持数组@rob.mI为您更新了一个很好的OOP设计,并删除了解包,因为它是不必要的。希望能有帮助@罗布·m Array ( [1200] => Array ( [0] => 1258 ) [1500] => Array ( ) [1900] => Array ( [0] => 1923 [1] => 1946 ) )