我想从表中声明3个随机行用作聚类质心
表名是data_kuesioner,包含列variable1,variable2,variable3,variable4,variable5
如何选择3个不同的随机行和值,然后调用其单个值,例如:
$centroid1cluster1 = (random row 1 variable 1)
$centroid2cluster1 = (random row 1 variable 2)
$centroid3cluster1 = (random row 1 variable 3)
$centroid4cluster1 = (random row 1 variable 4)
$centroid5cluster1 = (random row 1 variable 5)
$centroid1cluster2 = (random row 2 variable 1)
$centroid2cluster2 = (random row 2 variable 2)
$centroid3cluster2 = (random row 2 variable 3)
$centroid4cluster2 = (random row 2 variable 4)
$centroid5cluster2 = (random row 2 variable 5)
$centroid1cluster3 = (random row 3 variable 1)
$centroid2cluster3 = (random row 3 variable 2)
$centroid3cluster3 = (random row 3 variable 3)
$centroid4cluster3 = (random row 3 variable 4)
$centroid5cluster3 = (random row 3 variable 5)
等
答案 0 :(得分:1)
使用array_rand('$ var',2)
$ input = array(“ Neo”,“ Morpheus”,“ Trinity”,“ Cypher”,“ Tank”);
$ rand_keys = array_rand($ input,2);
echo $ input [$ rand_keys [0]]。 “ \ n”;
答案 1 :(得分:1)
要获得3行和随机行,可以在下面的查询中使用
$sql = $this->db->query("select variable1,variable2,variable3,variable4,variable5 from data_kuesioner order by RAND() limit 3");
echo'<pre>';print_r($sql->result());die;
使用方法链接
$this->db->select("variable1,variable2,variable3,variable4,variable5");
$this->db->from('data_kuesioner');
//$this->db->order_by('variable1','RANDOM');
//or
$this->db->order_by('rand()');
$this->db->limit(3);
$query = $this->db->get();
if ($query->num_rows() > 0) {
$result = $query->result_array();
$j = 1;
$dynamicVariable = array();
foreach ($result as $row) {
$dynamicVariable['centroid1cluster'.$j]= $row['variable1'];
$dynamicVariable['centroid2cluster'.$j] = $row['variable2'];
$dynamicVariable['centroid3cluster'.$j] = $row['variable3'];
$dynamicVariable['centroid4cluster'.$j] = $row['variable4'];
$dynamicVariable['centroid5cluster'.$j] = $row['variable5'];
$j++;
}
}
echo'<pre>';print_r($dynamicVariable);die;