回声只有独特的价值观

时间:2017-06-07 11:04:59

标签: php

我有一个打印出新闻稿的XML文件。我希望回收新闻稿发布后的所有年份,如档案。只有唯一的价值观以下代码打印出所有年份。像“2017,2017,2017,2016,2016,2015,2015,2014”以外的“2017,2016,2015,2014,2013”​​。

<?php
$file = "file.xml";
$xml = simplexml_load_file($file);

foreach ($xml->Release as $release) {
   $date = $release['PublishDateUtc']; /* $date is now in format yyyy-mm-ddT06:30:00 */
   $year = substr($date, 0, 4);
   echo $year;
}

?>

2 个答案:

答案 0 :(得分:2)

记下已打印的日期。

        <!DOCTYPE html>
<html ng-app="myapp" ng-controller="ticketcontroller">

<head>
    <title>Ticket</title>

    <link href="css/style.css" rel="stylesheet"
    type="text/css" media="all" />
    <link rel="stylesheet" href="css/flexslider.css"
    type="text/css" media="screen" property=""
    />
    <link rel="stylesheet" href="css/flexslider.css"
    type="text/css" media="screen" property=""
    />
    <script src="js/jquery-2.2.3.min.js"></script>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
    </script>
    <link href="//fonts.googleapis.com/css?family=Montserrat:400,700"
    rel="stylesheet">
</head>

<body>
    <div class="main">
        <h1>Ticket Details Forum</h1>

        <div class="w3_agile_main_grids">
            <section class="slider">
                <div class="flexslider">

                    <div class="agileits_w3layouts_main_grid">
                        <h3>Service Details</h3>
                        <form>
                            <span>
                                         <label>Category</label>
                                         <select  required="" ng-
                                          model="service">
                                             <option value="">Laptop 
                                            Repair</option>
                                             <option value="">Refrigerator 
                                             repair</option>
                                             <option value="">Microwave 
                                                  repair</option>


                                         </select>
                                     </span>
                            <span>
                                         <label>Priority</label>
                                         <select  required="" ng-
                                           model="priority">
                                             <option value="">Low</option>
                                             <option 
                                          value="">Normal</option>         
                                             <option value="">High</option>

                                         </select>
                                     </span>

                            <span>
                                         <label>Description</label>
                                        <input name="description" 
    type="text" placeholder="Enter Description here" ng-model="description">
                                     </span>

                            <div class="w3_agileits_submit">

                                <button class="btn btn-success" ng- click="submitdata()">submit</button>

                            </div>

                        </form>
                    </div>
            </section>
            </div>

        </div>

        <!-- flexSlider -->
        <script defer src="js/jquery.flexslider.js"></script>
        <script type="text/javascript">
            $(window).load(function() {
                $('.flexslider')
                    .flexslider({
                        animation: "slide",
                        start: function(
                            slider
                        ) {
                            $
                                (
                                    'body'
                                )
                                .removeClass(
                                    'loading'
                                );
                        }
                    });
            });
        </script>

</body>

</html>

<script>
    var ap = angular.module("myapp", []);
    ap.controller("ticketcontroller", [
                '$scope', '$http',
                '$window',
                function($scope, $http,
                    $window) {
                    $scope.submitdata =
                        function() {
                            $window.alert(
                                "dhbhhfgh);
                            }
                        }]);
</script> 

答案 1 :(得分:0)

试试这个

<?php
$file        = "file.xml";
$xml         = simplexml_load_file($file);
$yearArray   = array();
$uniqueArray = array();
foreach ($xml->Release as $release) {
    $date        = $release['PublishDateUtc'];
    /* $date is now in format yyyy-mm-ddT06:30:00 */
    $year        = substr($date, 0, 4);
    $yearArray[] = $year;
}

$uniqueArray = array_unique($yearArray);
print_r($uniqueArray);
?>

初始化了2个额外的变量并在循环中创建了几年的数组。然后打印出唯一的数组。