codeigniter 如何在PHP中通过索引动态循环获取数据?

wyyhbhjk  于 2022-12-07  发布在  PHP
关注(0)|答案(1)|浏览(130)

i具有数据阵列DINAMIC,

$methods = [];
    $result [] = ''; //result callculate save this variable;
    foreach($data as $key=>$val){
        $methods[]= $val['list_method'];
    }

我有一个数组,我正在循环,结果如下所示:

Array
(
    [0] => Array
        (
            [rekomendasi] => 1
            [select] => 236
            [method] => Array
                (
                    [0] => Array
                        (
                            [id] => 0
                            [rating] => 0
                        )

                    [1] => Array
                        (
                            [id] => 629
                            [rating] => 443.2
                        )

                )

        )

    [1] => Array
        (
            [rekomendasi] => 1
            [select] => 234
            [method] => Array
                (
                    [0] => Array
                        (
                            [id] => 620
                            [rating] => 343.43
                        )

                    [1] => Array
                        (
                            [id] => 628
                            [rating] => 345.772
                        )

                )

        )

)
1

在这种情况下,我根据索引进行计算。我做了一个如下的示例:

$result1 = 1 / (1 + (pow( 10 , ( index[0][method][0][rating] - index[0][method][1][rating])/400);
$result2 = 1 / (1 + (pow( 10 , ( index[1][method][0][rating] - index[1][method][1][rating])/400);

以前我做了代码,但我仍然对如何使计算动态感到困惑?
感谢

iszxjhcz

iszxjhcz1#

尝试下一个代码...

$result = []; //set result as an array;
    foreach($data as $key=>$val){
        $result[] = Ratings($val["method"]); // save the results in the array
    }
    print_r($result); // print the $result array
    
    // function Ratings to hold the equation 
    function Ratings($method){
      $calculate = 1 / (1 + (pow( 10 , ( $method[0]["rating"] - $method[1]["rating"])/400);
      return $calculate;
    }

相关问题