Thursday, 6 September 2018

PHP Delete the last two elements of the multidimensional array with a subset

I have the folowing multidimensional array from which i want to delete the last two elements from the sub array(there may be any number of subarray).
    $data=  Array
(
    [0] => Array
        (
            [rowdata] => Array
                (
                    [0] => Array
                        (
                            [DOELGROEP] => 2
                            [KANTOOR] => 2
                            [OBLIGOCATEGORIE] => 3
                            [] => Overall NPS
                            [0] => Array
                                (
                                    [1] => npsorg
                                    [3] => npsdetbe
                                )
                        )

                    [1] => Array
                        (
                            [DOELGROEP] => 2
                            [KANTOOR] => 2
                            [OBLIGOCATEGORIE] => 3
                            [] => Overall NPS
                            [0] => Array
                                (
                                    [1] => npsorg
                                    [3] => npsdetbe
                                )

                        )

                )

            [reason] => column values are not correct
        )

)

Desired Output:
$data=  Array
(
    [0] => Array
        (
            [rowdata] => Array
                (
                    [0] => Array
                        (
                            [DOELGROEP] => 2
                            [KANTOOR] => 2
                            [OBLIGOCATEGORIE] => 3

                        )

                    [1] => Array
                        (
                            [DOELGROEP] => 2
                            [KANTOOR] => 2
                            [OBLIGOCATEGORIE] => 3

                        )

                )

            [reason] => column values are not correct
        )

)

what i have tried:
unset ($data[count($data)-2]);

the above code is not working i.e. its not removing the last two elements.what am i doing wrong?
Thanks in advance

Your code was not working unset ($data[count($data)-2]); because count($data)-2 = -1 so your array $data doesn't have a key -1, $data[-1]. You need to follow your array example $data[0]['rowdata'][0]['ab'].
If you are sure that you will have always that array 'design' go with this.
   <?php
         $data = Array(
            0 => Array(
                'rowdata' => Array(
                    0 => Array
                        (
                            'DOELGROEP' => 2,
                            'KANTOOR' => 2,
                            'OBLIGOCATEGORIE' => 3,
                            0 => Array
                                (
                                    1 => 'npsorg',
                                    3 => 'npsdetbe'
                                )
                        ),
                    1 => Array
                        (
                            'DOELGROEP' => 2,
                            'KANTOOR' => 2,
                            'OBLIGOCATEGORIE' => 3,
                            0 => Array
                                (
                                    1 => 'npsorg',
                                    3 => 'npsdetbe'
                                )

                        )

                ),
                    'reason' => 'column values are not correct'
                )
            );

 echo '<pre>';
echo '<h1>before</h1>';
        print_r($data);

    foreach($data[0]['rowdata'] as $k => $v){
       unset($data[0]['rowdata'][$k][0]);
    }

       echo '<h1>After</h1>';
        print_r($data);

         //unset($data[0]['rowdata'][0][0]);
        // unset($data[0]['rowdata'][1][0]);

Update: You can either,
unset($data[0]['rowdata'][0][0]);
unset($data[0]['rowdata'][1][0]);

Or
 foreach($data[0]['rowdata'] as $k => $v){
   unset($data[0]['rowdata'][$k][0]);
}

Updated example: example

0 comments:

Post a Comment