如何从静态数组制作动态数组

时间:2019-01-05 17:47:43

标签: php arrays

我在使用静态数组时遇到麻烦,我需要使用动态数组进行更新,有人可以帮助我吗?因为如果我有很多数据,并且必须创建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);

此图像代表我的结果 enter image description here 如您所见,我的代码确实是静态的,如果我有很多数据,那么很难 我也需要这样的结果 enter image description here

编辑 VörösAmadea的答案是正确的,但仍然缺少一些答案,关于结果的第二个问题仍然很多。有没有办法只显示一次? 正如您之前在我的图像上看到的那样,我需要删除重复的数据,因此每个显示的人只有1个,并将其存储到数组中。 enter image description here

我修改了VörösAmadea的一些代码,因为如果不进行修改,则会收到错误消息“未定义变量:概率”

这是我修改过的代码。

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"});

1 个答案:

答案 0 :(得分:0)

您可以像这样在for循环中生成变量名。
只需更改$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"});
}