sophie
sophie

Reputation: 1531

Filter the multiple dimension array base on parameters?

I am trying to filter more on start_date and 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; 
        } 
    ?> 

Example:

<?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 
        )

)

For Now I want to filter that the function can filter by start_date and end_date

so I change the

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

Anyone could How can I filter in the period of date?

Upvotes: 2

Views: 295

Answers (1)

NikM
NikM

Reputation: 83

I always convert my time data to timestamps, so you can easily calculate everything, after converting to a timestamp, it's just basic mathematics.

Your new function wil look something like this:

<?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; 
}

?>

For other notations etc, best check the strtotime() page on php.net! http://www.php.net/manual/en/function.strtotime.php

Off course you can also create it in the same function, so you don't have to write more functions:

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

Upvotes: 1

Related Questions