基本上我想做这样的事情:
$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$avg = array_sum($arr) / count($arr);
$callback = function($val){ return $val < $avg };
return array_filter($arr, $callback);
Run Code Online (Sandbox Code Playgroud)
这有可能吗?计算匿名函数之外的变量并在里面使用它?
为什么是$args未定义的变量,它已经在回调之外定义了?我该如何解决这个问题?
我收到了错误 Undefined variable: args in ...
网址查询: products/category/1
$query = explode("/", $_SERVER['QUERY_STRING']);
$controller = (sizeof($query) >= 1 && !empty($query[0])) ? $query[0] : null;
$method = sizeof($query) >= 2 ? $query[1] : null;
$args = sizeof($query) >= 3 ? array_slice($query, 2, sizeof($query)) : null;
if(is_null($controller)) {
header("Location: ".URL . "?products/all");
}
if($controller === "products") {
$gw = new ProductDataGateway();
$products = null;
if($method === "all")
$products = $gw->getAllProducts();
elseif($method === "category" && sizeof($args) >= 1) {
$products = …Run Code Online (Sandbox Code Playgroud)