【问题标题】:Filter the multiple dimension array base on parameters?根据参数过滤多维数组?
【发布时间】:2011-11-02 21:26:44
【问题描述】:

我正在尝试对 start_date 和 end date 进行更多过滤。

   <?php 
        /* 
         * filtering an array 
         */ 
        function filter_by_value ($array, $index, $value){ 
            if(is_array($array) && count($array)>0)  
            { 
                foreach(array_keys($array) as $key){ 
                    $temp[$key] = $array[$key][$index]; 

                    if ($temp[$key] == $value){ 
                        $newarray[$key] = $array[$key]; 
                    } 
                } 
              } 
          return $newarray; 
        } 
    ?> 

例子:

<?php 
$results = array (
  0 => 
  array (
    'a' => 'U/H',
    'b' => 'Modification',
    'date' => '02/11/11',
    'h' => '17:15:13',
    'fullname' => 'First1 Name1',
    'desc' => 'Descript ',
  ),
  1 => 
  array (
    'a' => 'J/M',
    'b' => '',
    'date' => '03/11/11',
    'h' => '17:18:27',
    'fullname' =>  'First1 Name1',
    'desc' => 'Descript more '
  ),
);

$nResults = filter_by_value($results, 'fullname', 'First1 Name1'); 
echo '<pre>'; 
print_r($nResults); 
echo '</pre>'; 


Array
(
    [0] => Array
        (
            [a] => U/H
            [b] => Modification
            [date] => 02/11/11
            [h] => 17:15:13
            [fullname] => First1 Name1
            [desc] => Descript 
        )

    [1] => Array
        (
            [a] => J/M
            [b] => 
            [date] => 03/11/11
            [h] => 17:18:27
            [fullname] => First1 Name1
            [desc] => Descript more 
        )

)

现在我想过滤该函数可以按 start_date 和 end_date 过滤

所以我改了

function filter_by_value ($array, $index, $value,$start_date,$end_date)

任何人都可以我如何在日期期间进行过滤?

【问题讨论】:

  • 最明显的方法是循环。你有没有试过,或者其他什么?
  • 是的,我正在尝试 :) 使用 array-filter php 但还没有得到
  • 好的,如果您可以发布您尝试过的任何内容,我会很乐意查看代码,看看您是否遗漏了什么。我看到你的编辑,但这是一个不同的功能,对吧?它不接受您要查找的参数。
  • 是的,我更新得更清楚了,谢谢

标签: php arrays date filter


【解决方案1】:

我总是将我的时间数据转换为时间戳,所以你可以轻松计算所有内容,转换为时间戳后,这只是基础数学。

您的新函数将如下所示:

<?php 
function filter_by_date($array, $index, $start_date, $end_date) {
      $newarray = array();
      if(is_array($array) && count($array)>0)  
        { 
            $start_time = strtotime($start_date); //make sure your date is written as  
            $end_time = strtotime($end_date);     // dd/mm/YYYY, will also work with           
                                                 //hours etc

            foreach(array_keys($array) as $key){ 
                $temp[$key] = strtotime($array[$key][$index]); //$index will be date

                if ($temp[$key] >= $start_time && $temp[$key] <= $end_time ){ 
                    $newarray[$key] = $array[$key]; 
                } 
            } 
          } 
      return $newarray; 
}

?>

对于其他符号等,最好检查 php.net 上的 strtotime() 页面! http://www.php.net/manual/en/function.strtotime.php

当然你也可以在同一个函数中创建它,这样你就不用写更多的函数了:

if ($index == ('date' || 'h')) {
   //the content of filter by date, or the function itself
} else {
   //the content of your first function
}

【讨论】:

  • 对不起,犯了一些错误,没有测试。此外,请务必在过滤之前将您的 newarray 定义为数组,否则您的 print_r 确实会告诉您,如果过滤器中没有任何内容,您没有提供数组。这次我测试了它,它可以工作。
  • 顺便说一句,看起来你的 YY 表示法也可以。如果您查看我给您的链接,您可以找到几乎所有可以询问 strtotime() 函数的内容。
猜你喜欢
  • 1970-01-01
  • 2020-05-30
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 2013-06-09
相关资源
最近更新 更多