Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

php - How to make dynamic array from static array

I have trouble with my static array, i need to update with dynamic array can someone help me ? because will be so hard if i have alot data and i must create 1 by 1.

$kriteria = [C1,C2,C3,C4,C5,C6];

$alternatif = [ALT1,ALT2,ALT,ALT4,ALT5,ALT6,ALT7];

$nEntropy = array();
    for ($i=0;$i<count($kriteria);$i++)
        {
            for ($j=0;$j<count($alternatif);$j++)
                {
                    $nEntropy[$i] = (((-1)/log(7)) *( 
                    ($probabilitas[0][0]*log($probabilitas[0][0]))+
                    ($probabilitas[1][0]*log($probabilitas[1][0]))+
                    ($probabilitas[2][0]*log($probabilitas[2][0]))+
                    ($probabilitas[3][0]*log($probabilitas[3][0]))+
                    ($probabilitas[4][0]*log($probabilitas[4][0]))+
                    ($probabilitas[5][0]*log($probabilitas[5][0]))+
                    ($probabilitas[6][0]*log($probabilitas[6][0]))
                ));
            }
        }

$nEntropy1 = array();
    for ($i=0;$i<count($kriteria);$i++)
        {
            for ($j=0;$j<count($alternatif);$j++)
                {
                    $nEntropy1[$i] = (((-1)/log(7)) *( 
                    ($probabilitas[0][1]*log($probabilitas[0][1]))+
                    ($probabilitas[1][1]*log($probabilitas[1][1]))+
                    ($probabilitas[2][1]*log($probabilitas[2][1]))+
                    ($probabilitas[3][1]*log($probabilitas[3][1]))+
                    ($probabilitas[4][1]*log($probabilitas[4][1]))+
                    ($probabilitas[5][1]*log($probabilitas[5][1]))+
                    ($probabilitas[6][1]*log($probabilitas[6][1]))
                ));
            }
        }

$nEntropy2 = array();
    for ($i=0;$i<count($kriteria);$i++)
        {
            for ($j=0;$j<count($alternatif);$j++)
                {
                    $nEntropy2[$i] = (((-1)/log(7)) *( 
                    ($probabilitas[0][2]*log($probabilitas[0][2]))+
                    ($probabilitas[1][2]*log($probabilitas[1][2]))+
                    ($probabilitas[2][2]*log($probabilitas[2][2]))+
                    ($probabilitas[3][2]*log($probabilitas[3][2]))+
                    ($probabilitas[4][2]*log($probabilitas[4][2]))+
                    ($probabilitas[5][2]*log($probabilitas[5][2]))+
                    ($probabilitas[6][2]*log($probabilitas[6][2]))
                ));
            }
        }

$nEntropy3 = array();
    for ($i=0;$i<count($kriteria);$i++)
        {
            for ($j=0;$j<count($alternatif);$j++)
                {
                    $nEntropy3[$i] = (((-1)/log(7)) *( 
                    ($probabilitas[0][3]*log($probabilitas[0][3]))+
                    ($probabilitas[1][3]*log($probabilitas[1][3]))+
                    ($probabilitas[2][3]*log($probabilitas[2][3]))+
                    ($probabilitas[3][3]*log($probabilitas[3][3]))+
                    ($probabilitas[4][3]*log($probabilitas[4][3]))+
                    ($probabilitas[5][3]*log($probabilitas[5][3]))+
                    ($probabilitas[6][3]*log($probabilitas[6][3]))
                ));
            }
        }

$nEntropyy4 = array();
    for ($i=0;$i<count($kriteria);$i++)
        {
            for ($j=0;$j<count($alternatif);$j++)
                {
                    $nEntropy4[$i] = (((-1)/log(7)) *( 
                    ($probabilitas[0][4]*log($probabilitas[0][4]))+
                    ($probabilitas[1][4]*log($probabilitas[1][4]))+
                    ($probabilitas[2][4]*log($probabilitas[2][4]))+
                    ($probabilitas[3][4]*log($probabilitas[3][4]))+
                    ($probabilitas[4][4]*log($probabilitas[4][4]))+
                    ($probabilitas[5][4]*log($probabilitas[5][4]))+
                    ($probabilitas[6][4]*log($probabilitas[6][4]))
                ));
            }
        }

$nEntropy5 = array();
    for ($i=0;$i<count($kriteria);$i++)
        {
            for ($j=0;$j<count($alternatif);$j++)
                {
                    $nEntropy5[$i] = (((-1)/log(7)) *( 
                    ($probabilitas[0][5]*log($probabilitas[0][5]))+
                    ($probabilitas[1][5]*log($probabilitas[1][5]))+
                    ($probabilitas[2][5]*log($probabilitas[2][5]))+
                    ($probabilitas[3][5]*log($probabilitas[3][5]))+
                    ($probabilitas[4][5]*log($probabilitas[4][5]))+
                    ($probabilitas[5][5]*log($probabilitas[5][5]))+
                    ($probabilitas[6][5]*log($probabilitas[6][5]))
                ));
            }
        }

showb($nEntropy);
showb($nEntropy1);
showb($nEntropy2);
showb($nEntropy3);
showb($nEntropy4);
showb($nEntropy5);

this image for my results enter image description here as you can see my code really static and so hard if i have alot data and also i need my results likes this enter image description here

EDIT the answers from V?r?s Amadea are correct, but there are still some that are lacking, my second question regarding the results is still in large numbers. is there a way to display it only once? as u can see on my image before, i need to remove duplicate data so only 1 for each is who will displayed and store it into array. enter image description here

I have modified a little code from V?r?s Amadea because if I don't modify it, I get an error "Undefined variable: probability"

this is the code that I have modified.

for($x=0;$x<1;$x++){
$nth = $x;
$entz = array();
for ($i=0;$i<count($kriteria);$i++)
    {
        for ($j=0;$j<count($alternatif);$j++)
            {
                ${"entz$nth"}[$i] = (((-1)/log(7)) *( 
                ($probabilitas[0][$nth]*log($probabilitas[0][$nth]))+
                ($probabilitas[1][$nth]*log($probabilitas[1][$nth]))+
                ($probabilitas[2][$nth]*log($probabilitas[2][$nth]))+
                ($probabilitas[3][$nth]*log($probabilitas[3][$nth]))+
                ($probabilitas[4][$nth]*log($probabilitas[4][$nth]))+
                ($probabilitas[5][$nth]*log($probabilitas[5][$nth]))+
                ($probabilitas[6][$nth]*log($probabilitas[6][$nth]))
                ));
            }
        }
    }
showb(${"entz$nth"});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can generate variable names in for loops like this.
Just change the value of $how_many_i_want.

$how_many_i_want = 3;

for($x=0;$x<$how_many_i_want;$x++){
    generate_entropy($x);
}

function generate_entropy($nth){
    $kriteria = ['C1','C2','C3','C4','C5','C6'];
    $alternatif = ['ALT1','ALT2','ALT','ALT4','ALT5','ALT6','ALT7'];

    ${"nEntropy$nth"} = array();
    for ($i=0;$i<count($kriteria);$i++){
        for ($j=0;$j<count($alternatif);$j++){
            ${"nEntropy$nth"}[$i] = (((-1)/log(7)) *(
                    ($probabilitas[0][$nth]*log($probabilitas[0][$nth]))+
                    ($probabilitas[1][$nth]*log($probabilitas[1][$nth]))+
                    ($probabilitas[2][$nth]*log($probabilitas[2][$nth]))+
                    ($probabilitas[3][$nth]*log($probabilitas[3][$nth]))+
                    ($probabilitas[4][$nth]*log($probabilitas[4][$nth]))+
                    ($probabilitas[5][$nth]*log($probabilitas[5][$nth]))+
                    ($probabilitas[6][$nth]*log($probabilitas[6][$nth]))
                ));
        }
    }
    showb(${"nEntropy$nth"});
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...