使用此查询我得到了这个数组。但有些行重复一些不同的列。我想要做的是将这些不同的列作为父数组的多维数组中的单独数组。我尝试用Mysql group_by distinct子句来做这件事,但我无法做到。我认为最好的方法是通过php实现它。请给我一些帮助。谢谢 Myquery
$query = "SELECT "
. "a.emp_nmbr, a.emp_frstNm, a.emp_lstNm, a.emp_lclFrn, b.emp_dsgntn, b.emp_dprtmnt, b.emp_allwncs, b.emp_bscSlry, b.emp_sts, c.emp_lvsCtgry, c.emp_tknFrm, c.emp_tknTo "
. "FROM "
. "employee a "
. "LEFT JOIN "
. "emp_details b "
. "ON "
. "a.emp_nmbr = b.emp_nmbr "
. "LEFT JOIN "
. "leaves_details c "
. "ON "
. "a.emp_nmbr = c.emp_nmbr "
. "WHERE emp_lclFrn = 0 ";
. "LIMIT $page, $perPage ";
codeigniter模型函数执行查询。
function count_and_data($query) {
$query = $this->db->query($query);
$result = $query->result();
$count = $query->num_rows();
$data['count'] = $count;
$data['rows'] = $result;
return $data;
}
我得到的结果数组
Array
(
[count] => 16
[rows] => Array
(
[0] => stdClass Object
(
[emp_nmbr] => ophl3
[emp_frstNm] => aaaaaaaa
[emp_lstNm] => saaa
[emp_lclFrn] => 0
[emp_dsgntn] => test
[emp_dprtmnt] => 0
[emp_allwncs] => a:2:{i:0;a:2:{s:7:"alwncNm";s:4:"food";s:9:"alwncAmnt";s:3:"456";}i:1;a:2:{s:7:"alwncNm";s:0:"";s:9:"alwncAmnt";s:0:"";}}
[emp_bscSlry] => 2555
[emp_sts] => 4
[emp_lvsCtgry] => 1
[emp_tknFrm] => 20141222
[emp_tknTo] => 20141223
)
[1] => stdClass Object
(
[emp_nmbr] => ophl3
[emp_frstNm] => aaaaaaaa
[emp_lstNm] => saaa
[emp_lclFrn] => 0
[emp_dsgntn] => test
[emp_dprtmnt] => 0
[emp_allwncs] => a:2:{i:0;a:2:{s:7:"alwncNm";s:4:"food";s:9:"alwncAmnt";s:3:"456";}i:1;a:2:{s:7:"alwncNm";s:0:"";s:9:"alwncAmnt";s:0:"";}}
[emp_bscSlry] => 2555
[emp_sts] => 4
[emp_lvsCtgry] => 3
[emp_tknFrm] => 20141223
[emp_tknTo] => 20141224
)
[2] => stdClass Object
(
[emp_nmbr] => ophl3
[emp_frstNm] => aaaaaaaa
[emp_lstNm] => saaa
[emp_lclFrn] => 0
[emp_dsgntn] => test
[emp_dprtmnt] => 0
[emp_allwncs] => a:2:{i:0;a:2:{s:7:"alwncNm";s:4:"food";s:9:"alwncAmnt";s:3:"456";}i:1;a:2:{s:7:"alwncNm";s:0:"";s:9:"alwncAmnt";s:0:"";}}
[emp_bscSlry] => 2555
[emp_sts] => 4
[emp_lvsCtgry] => 4
[emp_tknFrm] => 20141224
[emp_tknTo] => 20141231
)
[3] => stdClass Object
(
[emp_nmbr] => ophl14
[emp_frstNm] => qqq
[emp_lstNm] => qqqq
[emp_lclFrn] => 0
[emp_dsgntn] => test
[emp_dprtmnt] => 0
[emp_allwncs] => a:2:{i:0;a:2:{s:7:"alwncNm";s:5:"qqqqq";s:9:"alwncAmnt";s:4:"2222";}i:1;a:2:{s:7:"alwncNm";s:0:"";s:9:"alwncAmnt";s:0:"";}}
[emp_bscSlry] => 2222
[emp_sts] => 3
[emp_lvsCtgry] =>
[emp_tknFrm] =>
[emp_tknTo] =>
)
[4] => stdClass Object
(
[emp_nmbr] => ophl1
[emp_frstNm] => yas
[emp_lstNm] => yas
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvsCtgry] =>
[emp_tknFrm] =>
[emp_tknTo] =>
)
[5] => stdClass Object
(
[emp_nmbr] => ophl2
[emp_frstNm] => test
[emp_lstNm] => test
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvsCtgry] =>
[emp_tknFrm] =>
[emp_tknTo] =>
)
[6] => stdClass Object
(
[emp_nmbr] => ophl4
[emp_frstNm] => yaa
[emp_lstNm] => yaaa
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvsCtgry] =>
[emp_tknFrm] =>
[emp_tknTo] =>
)
[7] => stdClass Object
(
[emp_nmbr] => ophl5
[emp_frstNm] => sdfds
[emp_lstNm] => sdfsdf
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvsCtgry] =>
[emp_tknFrm] =>
[emp_tknTo] =>
)
[8] => stdClass Object
(
[emp_nmbr] => ophl6
[emp_frstNm] => fsdfsdf
[emp_lstNm] => sfsdfsdf
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvsCtgry] =>
[emp_tknFrm] =>
[emp_tknTo] =>
)
[9] => stdClass Object
(
[emp_nmbr] => ophl7
[emp_frstNm] => fsdfsdf
[emp_lstNm] => sfsdfsdf
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvsCtgry] =>
[emp_tknFrm] =>
[emp_tknTo] =>
)
[10] => stdClass Object
(
[emp_nmbr] => ophl8
[emp_frstNm] => yyyyyyyyy
[emp_lstNm] => yyyyyyy
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvsCtgry] =>
[emp_tknFrm] =>
[emp_tknTo] =>
)
[11] => stdClass Object
(
[emp_nmbr] => ophl9
[emp_frstNm] => sfdsf
[emp_lstNm] => sfdsdfsdf
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvsCtgry] =>
[emp_tknFrm] =>
[emp_tknTo] =>
)
[12] => stdClass Object
(
[emp_nmbr] => ophl10
[emp_frstNm] => test
[emp_lstNm] => test
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvsCtgry] =>
[emp_tknFrm] =>
[emp_tknTo] =>
)
[13] => stdClass Object
(
[emp_nmbr] => ophl11
[emp_frstNm] => test
[emp_lstNm] => test
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvsCtgry] =>
[emp_tknFrm] =>
[emp_tknTo] =>
)
[14] => stdClass Object
(
[emp_nmbr] => ophl12
[emp_frstNm] => ssss
[emp_lstNm] => sss
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvsCtgry] =>
[emp_tknFrm] =>
[emp_tknTo] =>
)
[15] => stdClass Object
(
[emp_nmbr] => ophl13
[emp_frstNm] => yaas
[emp_lstNm] => yaas
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvsCtgry] =>
[emp_tknFrm] =>
[emp_tknTo] =>
)
)
)
我期待的是
Array
(
[count] => 16
[rows] => Array
(
[0] => stdClass Object
(
[emp_nmbr] => ophl3
[emp_frstNm] => aaaaaaaa
[emp_lstNm] => saaa
[emp_lclFrn] => 0
[emp_dsgntn] => test
[emp_dprtmnt] => 0
[emp_allwncs] => a:2:{i:0;a:2:{s:7:"alwncNm";s:4:"food";s:9:"alwncAmnt";s:3:"456";}i:1;a:2:{s:7:"alwncNm";s:0:"";s:9:"alwncAmnt";s:0:"";}}
[emp_bscSlry] => 2555
[emp_sts] => 4
[emp_lvData]=>array(
array[0](
[emp_lvsCtgry] => 1
[emp_tknFrm] => 20141222
[emp_tknTo] => 20141223
)
array[1](
[emp_lvsCtgry] => 3
[emp_tknFrm] => 20141223
[emp_tknTo] => 20141224
)
array[2](
[emp_lvsCtgry] => 4
[emp_tknFrm] => 20141224
[emp_tknTo] => 20141231
)
)
[3] => stdClass Object
(
[emp_nmbr] => ophl14
[emp_frstNm] => qqq
[emp_lstNm] => qqqq
[emp_lclFrn] => 0
[emp_dsgntn] => test
[emp_dprtmnt] => 0
[emp_allwncs] => a:2:{i:0;a:2:{s:7:"alwncNm";s:5:"qqqqq";s:9:"alwncAmnt";s:4:"2222";}i:1;a:2:{s:7:"alwncNm";s:0:"";s:9:"alwncAmnt";s:0:"";}}
[emp_bscSlry] => 2222
[emp_sts] => 3
[emp_lvData]=>array(
array[0](
[emp_lvsCtgry] => 1
[emp_tknFrm] => 20141222
[emp_tknTo] => 20141223
)
)
)
[4] => stdClass Object
(
[emp_nmbr] => ophl1
[emp_frstNm] => yas
[emp_lstNm] => yas
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvData]=>array(
array[0](
[emp_lvsCtgry] => 1
[emp_tknFrm] => 20141222
[emp_tknTo] => 20141223
)
)
)
[5] => stdClass Object
(
[emp_nmbr] => ophl2
[emp_frstNm] => test
[emp_lstNm] => test
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvData]=>array(
array[0](
[emp_lvsCtgry] => 1
[emp_tknFrm] => 20141222
[emp_tknTo] => 20141223
)
)
)
[6] => stdClass Object
(
[emp_nmbr] => ophl4
[emp_frstNm] => yaa
[emp_lstNm] => yaaa
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvData]=>array(
array[0](
[emp_lvsCtgry] => 1
[emp_tknFrm] => 20141222
[emp_tknTo] => 20141223
)
)
)
[7] => stdClass Object
(
[emp_nmbr] => ophl5
[emp_frstNm] => sdfds
[emp_lstNm] => sdfsdf
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvData]=>array(
array[0](
[emp_lvsCtgry] => 1
[emp_tknFrm] => 20141222
[emp_tknTo] => 20141223
)
)
)
[8] => stdClass Object
(
[emp_nmbr] => ophl6
[emp_frstNm] => fsdfsdf
[emp_lstNm] => sfsdfsdf
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvData]=>array(
array[0](
[emp_lvsCtgry] => 1
[emp_tknFrm] => 20141222
[emp_tknTo] => 20141223
)
)
)
[9] => stdClass Object
(
[emp_nmbr] => ophl7
[emp_frstNm] => fsdfsdf
[emp_lstNm] => sfsdfsdf
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvData]=>array(
array[0](
[emp_lvsCtgry] => 1
[emp_tknFrm] => 20141222
[emp_tknTo] => 20141223
)
)
)
[10] => stdClass Object
(
[emp_nmbr] => ophl8
[emp_frstNm] => yyyyyyyyy
[emp_lstNm] => yyyyyyy
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvData]=>array(
array[0](
[emp_lvsCtgry] => 1
[emp_tknFrm] => 20141222
[emp_tknTo] => 20141223
)
)
)
[11] => stdClass Object
(
[emp_nmbr] => ophl9
[emp_frstNm] => sfdsf
[emp_lstNm] => sfdsdfsdf
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvData]=>array(
array[0](
[emp_lvsCtgry] => 1
[emp_tknFrm] => 20141222
[emp_tknTo] => 20141223
)
)
)
[12] => stdClass Object
(
[emp_nmbr] => ophl10
[emp_frstNm] => test
[emp_lstNm] => test
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvData]=>array(
array[0](
[emp_lvsCtgry] => 1
[emp_tknFrm] => 20141222
[emp_tknTo] => 20141223
)
)
)
[13] => stdClass Object
(
[emp_nmbr] => ophl11
[emp_frstNm] => test
[emp_lstNm] => test
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvData]=>array(
array[0](
[emp_lvsCtgry] => 1
[emp_tknFrm] => 20141222
[emp_tknTo] => 20141223
)
)
)
[14] => stdClass Object
(
[emp_nmbr] => ophl12
[emp_frstNm] => ssss
[emp_lstNm] => sss
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvData]=>array(
array[0](
[emp_lvsCtgry] => 1
[emp_tknFrm] => 20141222
[emp_tknTo] => 20141223
)
)
)
[15] => stdClass Object
(
[emp_nmbr] => ophl13
[emp_frstNm] => yaas
[emp_lstNm] => yaas
[emp_lclFrn] => 0
[emp_dsgntn] =>
[emp_dprtmnt] =>
[emp_allwncs] =>
[emp_bscSlry] =>
[emp_sts] =>
[emp_lvData]=>array(
array[0](
[emp_lvsCtgry] => 1
[emp_tknFrm] => 20141222
[emp_tknTo] => 20141223
)
)
)
)
)
答案 0 :(得分:1)
这是一个快速流程,可以帮助您实现您的要求。
// collect all the unique data here
$holder = array();
foreach($data['rows'] as $data) {
// check with emp_nmbr = ophl3
if( ( $key = _finder($data->emp_nmbr, $holder) ) === false ) {
$holder[] = array(
'emp_nmbr'=>$data->emp_nmbr,
..........
'emp_lvData'=>array(
array('emp_lvsCtgry'=>$data->emp_lvsCtgry)
..........
)
);
}
else {
// emp_nmbr = ophl3 found now Merge Data Here..
if( ! empty( $data->emp_lvsCtgry ) ) {
$holder[$key]['emp_lvData'] =
array_merge(
$holder[$key]['emp_lvData'],
array('emp_lvsCtgry'=>$data->emp_lvsCtgry));
}
}
}
// Search Value in Muliti-dimentional array
function _finder($key, $array) {
if( ! is_array($array) || count($array) == 0 ) return false;
foreach( $array as $k=>$arr ){
if( in_array( $key, $arr ) ) {
return $key;
}
}
return false;
}
$holder
保存所有唯一数组数据。