Using this query I am getting this array as a result. but some rows are repeating with some distinct columns. what I want to do is take those distinct columns in to a separate array as a multidimensional array of the parent array. I tried to do it with Mysql group_by distinct clause but I couldn't do it. I think best way is to make it by php. Please give me a little help with this thing. Thanks 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 model function to execute query.
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;
}
The result array I am getting
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] =>
)
)
)
What I am Expecting is
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
)
)
)
)
)
This is a quick flow that helps you to achieve your requirenment.
// 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;
}
Where $holder
holds all your unique array data.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.