【问题标题】:php array: return matching elementsphp数组:返回匹配的元素
【发布时间】:2011-04-25 21:00:50
【问题描述】:
 class Photos 
{
private $photos = array();

function add_photo($filename, $date, $lat, $long) 
{
  $this->photos[] = array('filename' => $filename, 'date' => $date, 'lat' => $lat,  'long' => $long);
    return $this;
}

function get_all() 
{
   return json_encode($this->photos);
}

function get_N($n) 
{
    return json_encode(array_slice($this->photos, 0, $n));
}

}

我想在我的类中使用另一个函数来返回某些具有特定日期的数组。使用 exif_read_data 函数从照片中提取日期。他们看起来像这样:2011:04:01 16:12:23。我正在寻找的功能应该返回某个日期的所有照片。所以我想知道我如何使函数返回,例如,所有带有日期戳的照片看起来像2011:04:01 xx:xx:xx。希望你明白我的意思。提前致谢!

【问题讨论】:

    标签: php arrays


    【解决方案1】:

    应该这样做:

    class Photos 
    {
        private $photos = array();
    
        function add_photo($filename, $date, $lat, $long) { /* ... */ }
    
        function get_all() { /* ... */ }
    
        function get_N($n) { /* ... */ }
    
        function get_by_date($date)
        {
            $result = array();
    
            foreach ($this->photos as $photo)
            {
                if (strpos($photo['date'], $date) === 0)
                {
                    $result[] = $photo;
                }
            }
    
            return $result;
        }
    }
    
    Photos::get_by_date('2011:04:01');
    

    您可能还想查看array_filter() 函数。


    function get_all_dates()
    {
        $result = array();
    
        foreach ($this->photos as $photo)
        {
            $result[substr($photo['date'], 0, 10)] = null;
        }
    
        return array_keys($result);
    }
    

    function get_all_dates_with_count()
    {
        $result = array();
    
        foreach ($this->photos as $photo)
        {
            $date = substr($photo['date'], 0, 10);
    
            if (empty($result[$date]))
            {
                $result[$date] = 0;
            }
    
            ++$result[$date];
        }
    
        return $result;
    }
    

    【讨论】:

    • 关于 array_filter() 函数的好注释,但这实际上与你在这里所做的一样。无论如何,您最终都会编写一个比较日期的函数。
    • @Compeek:在 PHP 5.3 中,您可以定义 lambda 函数,但是是的。我刚刚提到是因为 OP 可能想要对他的数据执行类似的过滤器。 :)
    • 用奖金信息给出答案永远不会受到伤害。 :)
    • hmm,后续问题:有什么方法可以创建一个函数来返回包含任何照片的所有日期?
    • 顺便说一句,我如何计算每个日期的照片数量?我想返回每个日期的计数。
    【解决方案2】:
    function get_by_date($date) {
        $date = strftime('%D', strtotime($date));
        $photos = array();
        foreach($this->photos as $photo) {
            if(strftime('%D', strtotime($photo['date'])) == $date) {
                $photos[] = $photo;
            }
        }
        return $photos;
    }
    

    【讨论】:

      【解决方案3】:

      我只是检查前 10 个字符是否相同:

      public function getByDate($date) {
          $return = array();
          foreach ($this->photos as $photo) {
              if ($date == substr($photo['date'], 0, 10)) {
                  $return[] = $photo;
              }
          }
      
          return $return;
      }
      

      如果您使用的是 PHP 5.3,您可以使用 array_filter 和闭包来做到这一点:

      public function getByDate($date) {
          return array_filter($this->photos, function($photo) use($date) {
              return $date == substr($photo['date'], 0, 10);
          });
      }
      

      【讨论】:

        猜你喜欢
        • 2012-10-25
        • 1970-01-01
        • 2017-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-16
        • 2013-02-19
        • 2016-10-31
        相关资源
        最近更新 更多