php - 如何在 JSON 中打印 max() 值
问题描述
如何在JSON
3 个函数的表单结果中打印最高值。
控制器 :
class TimeFrameStatisticsController extends Controller
{
public function one()
{
return 3;
}
public function two()
{
return 1;
}
public function three()
{
return 4;
}
//FUNCTION FOR 4rth to get HIGHEST value
public function HighestValue() {
$func_names = ['three','one','two'];
// Call each method and store result.
foreach($func_names as $name) {
$results[$name] = $name();
}
$max = max($results);
$max_funcs = array_keys($results, $max);
return response()->json($max_funcs);
}
}
public function HighestValue()
API的 OUTPUT 错误:调用未定义的函数public function one()
和
其他 3 个函数由 API 生成:
我的问题是我该怎么办?我应该创建对象以及如何创建?
解决方案
此功能为我提供了 3 种不同的最高值的完美结果。
public function allCmp()
{
$firstTradeValue = $this->one();
$lastTradeValue = $this->two();
$otherTradeValue = $this->three();
//Set all in one array
$allTrades = array("firstHourTrades" => $firstTradeValue, "lastHourTrades" => $lastTradeValue, "otherHoursTrades" => $otherTradeValue );
//Get the highest value
$highestValue = max($allTrades);
//Get the corresponding key
$key = array_search($highestValue, $allTrades);
return response()->json($key);
}
推荐阅读
- html - 右侧的额外空间
- git - 强制推送到不同的分支时,Git 不会将新文件推送到远程
- sml - 在 SML 中将 int 转换为 real
- sql - 使用动态列名 SQL Server 连接
- c - ~(char)((unsigned char) ~0 >> 1) 到底发生了什么?
- javascript - 是否可以使用 ajax 执行任何 php 函数
- c# - 登录到第二个表单后如何传递名称变量
- javascript - 标签内容文件图像预览脚本不起作用
- html - Django - 一个 href 重复目标
- docker - 应用程序依赖于为环境变量采购 secrets.sh。如何在我的 Dockerfile 中实现这一点?