首页 > 解决方案 > 如何在 JSON 中打印 max() 值

问题描述

如何在JSON3 个函数的表单结果中打印最高值。

控制器 :

 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 生成:

在此处输入图像描述

我的问题是我该怎么办?我应该创建对象以及如何创建?

标签: phplaravel

解决方案


此功能为我提供了 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);

}

推荐阅读