There was a problem with filtering data in an array.
The bottom line is: 1. User GET sends filtering data.

index.php?type=filter&date=06.12&time=17:30&name=&price=&count=&address=&comment= 

2. We have an array

 array (size=76) 0 => array (size=8) 0 => string '06.2012' (length=7) 1 => string '17:59' (length=5) 2 => string 'Велик б' (length=13) 3 => string '150' (length=3) 4 => string '1' (length=1) 5 => string 'Киев' (length=8) 6 => string 'Доставка' (length=16) 7 => string '1471962977 ' (length=11) 1 => array (size=8) 0 => string '06.2011' (length=7) 1 => string '17:59' (length=5) 2 => string 'Велик б' (length=13) 3 => string '150' (length=3) 4 => string '1' (length=1) 5 => string 'Киев' (length=8) 6 => string 'Доставка' (length=16) 7 => string '1471962720 ' (length=11) 2 => array (size=8) 0 => string '06.2010' (length=7) 1 => string '17:59' (length=5) 2 => string 'Велик б' (length=13) 3 => string '150' (length=3) 4 => string '1' (length=1) 5 => string 'Киев' (length=8) 6 => string 'Доставка' (length=16) 7 => string '1471962116 ' (length=11) 
  1. It is necessary to filter the data of the original array and return the filtered.
    There are suggestions that you need to go through all the combinations. For example, if the date came together, check with the time, etc. But this option is time consuming and confusing. How can you do differently?

Closed due to the fact that off-topic participants VenZell , aleksandr barakin , user194374, Denis , sercxjo 25 Aug '16 at 8:15 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - aleksandr barakin, Community Spirit, Denis
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Specify exactly which filtering rules should be applied? - VenZell
  • Dig in the direction of filter_var_array - Urmuz Tagizade
  • If $ _GET ['data'] == $ array [0] [0] check by the next field, if not, check by the second one. Return an array of matches - Mykola Shkit
  • You can try this hellish version of github.com/ReactiveX/RxPHP - rjhdby
  • Under the word "filter" you mean to exclude doubles? Or sort by date-time? - Sergiks

1 answer 1

 Class Filter{ private $out = array(); public function __construct($input){ $this->out = $input; } public function byField($field, $pattern){ $temp = array(); foreach ($this->out as $value){ if(isMatch($value[$field], $pattern)){ $temp[] = $value; } } $this->out = $temp; return $this; } public function getResult(){ return $this->out; } private function isMatch($value, $pattern){ #тут производите проверку, попадает ли под условия фильтра и возвращаете true или false } } 

using

 $myArray = array(.....); $filter = new Filter($myArray); $filtered = $filter ->byField('date', $_GET['date']) ->byField('time', $_GET['time']) .... ->byField('count', $_GET['count']) ->getResult; 
  • And if there is no value in get empty it will return false - Mykola Shkit
  • @MykolaShkit if I had thrown a fully working class here for your specific task, then a hefty sheet would have come out. Naturally, no one exempts from control of input data and other nuances of your particular case. - rjhdby
  • @MykolaShkit You can, for example, in the isMatch method add the first line if ($ pattern === null) return true; In general, the ways of the car and a small truck - rjhdby
  • Thank you, you really helped, but you still need to add a check whether the array $ temp is empty - Mykola Shkit