0

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
                              )
                           )
                )

        )

)
6
  • I think you can use a foreach loop the sql result and get you want. Commented Dec 22, 2014 at 6:44
  • @LLL - But how to remove those repeating rows Commented Dec 22, 2014 at 6:47
  • First, I think your structure should be change something. And I have write a sample code to push same key value to a array. I hope that will help for your. gist.github.com/c11c1fbbd48321354d1f.git Commented Dec 22, 2014 at 6:52
  • @LLL - your link is not working can please check it. Commented Dec 22, 2014 at 6:54
  • gist.github.com/lighter/c11c1fbbd48321354d1f Commented Dec 22, 2014 at 7:00

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I feel this will do the work but. still I am getting some errors
array_merge(): Argument #1 is not an array (this error two times) Undefined index: ophl3 both errors are in this line $holder[$key]['emp_lvData'] = array_merge($holder[$key]['emp_lvData'], array('' => $data->emp_lvsCtgry));
@Yasitha you have to check the array first, because in some cases you don't have value in emp_lvsCtgry
Sorry for disturbing you. I checked your updated code. it is returning only emp_lvsCtgry for each employee. if I tried to add other details to $holder array then it is again giving the same three errors as above. I change your code from $holder[] = array( 'emp_lvData' => array( array('emp_lvsCtgry' => $data->emp_lvsCtgry) ) to $holder[] = array( 'emp_nmbr' => $data->emp_nmbr, 'emp_lvData' => array( array('emp_lvsCtgry' => $data->emp_lvsCtgry) )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.