我有这样的功能
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变量时一样
答案 0 :(得分:0)
if ($initialClass!=$newClass) {
$result=kmeans($data, $newCentroidGenerated, $newClass, $indexOfData);
}
return $result;
像这样返回。