0

Using this query, I am getting this array as a result, but some rows are repeating with some distinct columns. I want to take those distinct columns in to a separate array as a multidimensional array of the parent array. I tried to do it with a Mysql group_by distinct clause, but I couldn't do it. I think best way is to make it by PHP.

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

        )

)
0

2 Answers 2

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

Rather than fumbling with grouping, and keeping track of unique nmbr values, you can let MySQL do that job. Because databases can only return 2d payloads, you will either need to loop over each object in the result array and json_decode the accumulated leaves data in the model method or ensure that that process is done in another layer.

$query = <<<SQL
SELECT 
    emp.emp_nmbr,
    emp.emp_frstNm,
    emp.emp_lstNm,
    emp.emp_lclFrn,
    det.emp_dsgntn,
    det.emp_dprtmnt,
    det.emp_allwncs,
    det.emp_bscSlry,
    det.emp_sts,
    JSON_ARRAYAGG(
        JSON_OBJECT(
            'emp_lvsCtgry', lvs.emp_lvsCtgry,
            'emp_tknFrm', lvs.emp_tknFrm,
            'emp_tknTo', lvs.emp_tknTo
        )
    ) emp_lvData
FROM employee emp
LEFT JOIN emp_details det USING (emp_nmbr)
LEFT JOIN leaves_details lvs USING (emp_nmbr)
WHERE emp.emp_lclFrn = 0
GROUP BY 
    emp.emp_nmbr,
    emp.emp_frstNm,
    emp.emp_lstNm,
    emp.emp_lclFrn,
    det.emp_dsgntn,
    det.emp_dprtmnt,
    det.emp_allwncs,
    det.emp_bscSlry,
    det.emp_sts
LIMIT $page, $perPage
SQL;

Related advice:

Comments

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.