Home > Mobile >  how to get data by indexing dinamic looph in php?
how to get data by indexing dinamic looph in php?

Time:07-15

i have data array dinamic,

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

I have an array that I am looping, the result is like below :

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

in this case I make calculations based on index. i made a sample like this :

$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);

Previously I made the code but I'm still confused about how to make the calculation dynamic?

thanks

CodePudding user response:

Try the next code...

    $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;
    }
  • Related