繁体   English   中英

在If-else Scoop中返回数组

Returning an Array in If-else Scoop

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

我有这样的功能

function kmeans($data, $centroid, $initialClass, $indexOfData){
$result=array();
$value=array();
for ($i=0; $i < count($data); $i++) { 
    $distance=array();
    for ($j=0; $j < count($centroid); $j++) { 
        $distance[$j]=sqrt(pow(($data[$i][$indexOfData]-$centroid[$j]),2));
    }
    if(array_search(min($distance), $distance)==0){
        $newClass[$i]="Tidak Laris";
        $result[0][]=$data[$i];
        $value[0][]=$data[$i][$indexOfData];
    }elseif (array_search(min($distance), $distance)==1) {
        $newClass[$i]="Agak Laris";
        $result[1][]=$data[$i];
        $value[1][]=$data[$i][$indexOfData];
    }elseif (array_search(min($distance), $distance)==2) {
        $newClass[$i]="Sangat Laris";
        $result[2][]=$data[$i];
        $value[2][]=$data[$i][$indexOfData];
    }
}
for ($i=0; $i < count($centroid); $i++) { 
    $newCentroidGenerated[$i]=@divide((array_sum($value[$i])),(count($value[$i])));
}
if ($initialClass!=$newClass) {
    kmeans($data, $newCentroidGenerated, $newClass, $indexOfData);
}else{
    return $result;
}

}

当我返回值时,只包含这样的数字

1

但是,当我尝试对变量variabel $ result进行打印时,它在其中包含许多项

Array ( [0] => Array ( [0] => Array ( [0] => 376219431015 [id_barang] => 376219431015 [1] => 8 [total] => 8 [2] => Samsung 14D4E [nama_barang] => Samsung 14D4E ) [1] => Array ( [0] => 438311046806 [id_barang] => 438311046806 [1] => 8 [total] => 8 [2] => Compaq CQ41-224tx [nama_barang] => Compaq CQ41-224tx ) [2] => Array ( [0] => 530556135122 [id_barang] => 530556135122 [1] => 8 [total] => 8 [2] => HP CK0006tx [nama_barang] => HP CK0006tx ) [3] => Array ( [0] => 613539171530 [id_barang] => 613539171530 [1] => 2 [total] => 2 [2] => Lenovo IP230 [nama_barang] => Lenovo IP230 ) [4] => Array ( [0] => 641020761998 [id_barang] => 641020761998 [1] => 4 [total] => 4 [2] => HP CK0004tx [nama_barang] => HP CK0004tx ) [5] => Array ( [0] => 765665463553 [id_barang] => 765665463553 [1] => 1 [total] => 1 [2] => Lenovo IP241 [nama_barang] => Lenovo IP241 ) [6] => Array ( [0] => 789851346783 [id_barang] => 789851346783 [1] => 9 [total] => 9 [2] => Toshiba Canvio 1Tb [nama_barang] => Toshiba Canvio 1Tb ) [7] => Array ( [0] => 829895569176 [id_barang] => 829895569176 [1] => 1 [total] => 1 [2] => ASUS ROG 14XDGM23 [nama_barang] => ASUS ROG 14XDGM23 ) [8] => Array ( [0] => WDBUZG0010BBK [id_barang] => WDBUZG0010BBK [1] => 3 [total] => 3 [2] => Western Digital HD Eks. 1TB [nama_barang] => Western Digital HD Eks. 1TB ) ) [2] => Array ( [0] => Array ( [0] => 431697173117 [id_barang] => 431697173117 [1] => 33 [total] => 33 [2] => HP CK0007tx [nama_barang] => HP CK0007tx ) [1] => Array ( [0] => 525666119354 [id_barang] => 525666119354 [1] => 26 [total] => 26 [2] => Lenovo IP210 [nama_barang] => Lenovo IP210 ) [2] => Array ( [0] => 809216329057 [id_barang] => 809216329057 [1] => 25 [total] => 25 [2] => ASUS A442UR [nama_barang] => ASUS A442UR ) ) [1] => Array ( [0] => Array ( [0] => 913872680267 [id_barang] => 913872680267 [1] => 14 [total] => 14 [2] => LCD Monitor 14 Inc [nama_barang] => LCD Monitor 14 Inc ) ) )

我希望返回值是数组,就像我在print_r $ result变量时那样

1 个回复
if ($initialClass!=$newClass) {
    $result=kmeans($data, $newCentroidGenerated, $newClass, $indexOfData);
}
return $result;

这样回来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题
 
粤ICP备18138465号  © 2020-2021 STACKOOM.COM