Jam*_*mad 0 php group-by codeigniter
我试图在阵列中获得具有相同ID的记录.我尝试了"Group_by"查询,但结果是单个数组.请参阅下图中的记录.
查询:
$this->db->select('typeID,projectID,typeName,typeSize,dimensionWidth,dimensionHeight');
$this->db->from('propertytype');
$this->db->group_by('projectID');
$rec=$this->db->get()->result();
echo "<pre>";print_r($rec);exit();
Run Code Online (Sandbox Code Playgroud)
结果:
Array
(
[0] => stdClass Object
(
[typeID] => 1
[projectID] => 1
[typeName] => Residential
[typeSize] => 5 MARLA
[dimensionWidth] => 125
[dimensionHeight] => 125
)
[1] => stdClass Object
(
[typeID] => 7
[projectID] => 2
[typeName] => Residential
[typeSize] => 5 MARLA
[dimensionWidth] => 26
[dimensionHeight] => 50
)
)
Run Code Online (Sandbox Code Playgroud)
我希望结果像二维数组一样.
Array(
[0]=> stdClass object(
Array(...)
Array(...)
Array(...)
Array(...)
)
[1]=> stdClass object(
Array(...)
Array(...)
Array(...)
Array(...)
)
)
Run Code Online (Sandbox Code Playgroud)
告诉我我在哪里弄错了?任何人都可以帮助我.我会非常感激的.
该GROUP BY子句通常用于Aggregate Functions返回每个组的汇总值.GROUP BY没有聚合的子句与使用类似SELECT DISTINCT.......该GROUP BY条款不订购数据.
我希望这可以帮到你:
$this->db->select('projectID');
$this->db->from('propertytype');
$this->db->distinct('projectID');
$rec=$this->db->get()->result();
$arr=array();
foreach ($rec as $r) {
$this->db->select('typeID,projectID,typeName,typeSize,dimensionWidth,dimensionHeight');
$this->db->from('propertytype');
$this->db->where('projectID',$r->projectID);
$rec=$this->db->get()->result();
$arr[]=$rec;
}
echo "<pre>";print_r($arr);exit();
Run Code Online (Sandbox Code Playgroud)