PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php // цены с БД в редисе $cacheData = [ 0 => [ "id" => 1, "id_company" => 1, "id_wheat" => 1, "no_nds" => 15.5, "yes_nds" => 10.0, "wheat" => [ "id" => 1, "id_company" => 1, "id_region" => 1, "class" => "3-й класс", "type" => "three", "year" => "2022/23", "gluten" => "23", "idk" => "85", "clop" => "2", "protein" => "15.3", "cp" => "220", "nature" => "760", "humidity" => "14", ], ], 1 => [ "id" => 2, "id_company" => 1, "id_wheat" => 2, "no_nds" => 15.1, "yes_nds" => 10.0, "wheat" => [ "id" => 2, "id_company" => 1, "id_region" => 1, "class" => "3-й класс", "type" => "three", "year" => "2022/23", "gluten" => "23", "idk" => "85", "clop" => "", "protein" => "14.5", "cp" => "220", "nature" => "760", "humidity" => "14", ], ], ]; // наши рамки $wheatArrParams = [ 'three' => [ 'class' => '3-й класс', 'type' => 'three', 'gluten' => [23, 23], 'idk' => [80, 90], 'clop' => [2, 3], 'protein' => [13.0, 15.3], 'cp' => [200, 280], 'nature' => [750, 760], 'humidity' => [13, 14], ], 'four' => [ 'class' => '4-й класс', 'type' => 'four', 'gluten' => [15, 21], 'idk' => [85, 95], 'clop' => [2, 3], 'protein' => [11.0, 13.0], 'cp' => [200, 230], 'nature' => [710, 750], 'humidity' => [13, 14], ] ]; // то что ищем $arr = [ 'gluten' => 23, 'idk' => 80, 'protein' => 13.1, 'cp' => 200, 'nature' => 220, 'humidity' => 13, ]; // ====================== // сам метод $foundItems = array_reduce($cacheData, function($carry, $item) use ($wheatArrParams, $arr) { $wheat = $item['wheat']; if ( $wheat['gluten'] == $arr['gluten'] && $wheat['idk'] >= $wheatArrParams[$wheat['type']]['idk'][0] && $wheat['idk'] <= $wheatArrParams[$wheat['type']]['idk'][1] && $wheat['clop'] >= $wheatArrParams[$wheat['type']]['clop'][0] && $wheat['clop'] <= $wheatArrParams[$wheat['type']]['clop'][1] && $wheat['protein'] >= $arr['protein'] && $wheat['cp'] >= $wheatArrParams[$wheat['type']]['cp'][0] && $wheat['cp'] <= $wheatArrParams[$wheat['type']]['cp'][1] && $wheat['nature'] >= $wheatArrParams[$wheat['type']]['nature'][0] && $wheat['nature'] <= $wheatArrParams[$wheat['type']]['nature'][1] && $wheat['humidity'] >= $wheatArrParams[$wheat['type']]['humidity'][0] && $wheat['humidity'] <= $wheatArrParams[$wheat['type']]['humidity'][1] ) { $carry[] = $item; } return $carry; }, []); print_r($foundItems);
Show:  
Copy Clear