1

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

2
  • 1
    Why don't you use the answer from your previous question and add unset to that version? Commented Jan 7, 2014 at 10:26
  • @DainisAbols i tried that also :) Commented Jan 7, 2014 at 10:27

8 Answers 8

1

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

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

1 Comment

thanks jorge :) i was doing a silly mistake on my side thats why was not getting the required output.thanks again :)
1
unset($data[0]['rowdata'][0][0]);

Comments

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

Comments

1

As you know you are removing elements from multidimensional array you have to reach up to that dimension. After that you need to have all keys of that array. You can use array_keys. You will get an array of keys. Then by using it you can unset last two elements of an array. You have to use this in loop.

You have to achieve:

unset($data[0]['rowdata'][0]['ab']);
unset($data[0]['rowdata'][0][0]);

Comments

1

You should use end function which will work for non-numerical indexes. This should work I think :

for($i=0; $i<2; $i++){
    end($data[0]['rowdata'][0]);
    unset(key($data[0]['rowdata'][0]));
    reset($data[0]['rowdata'][0]);
}

Comments

1

@Mrcoder Same thing, except you should wrap this around a foreach function to parse all your "main" arrays.

$toDel = 2;
foreach($data[0]['rowdata'] as $main){
    for($i=0; $i<toDel; $i++){
        end($main);
        unset(key($main));
        reset($main);
    }
}

Comments

1

Try creating a custom filter function

function myFilter($val){
foreach($val as $key=>$val){
$len =  sizeof($val);   
  for($i=0;$i<$len;$i++){ 
     $val[$i] = array_splice($val[$i],0,-2);
  }
    }
 return $val;
}

Then call array_map

$data = array_map('myFilter',$data);
print_r($data);
exit();

You can see more info here

1 Comment

This only works for a single sub array. i will update my answer to your new requirement
1

If the three keys you always want to keep are DOELGROEP, KANTOOR and OBLIGOCATEGORIE - the following will work.

$keep_indexes = array('DOELGROEP', 'KANTOOR', 'OBLIGOCATEGORIE');

foreach($data[0]['rowdata'] as $key => $val) {
    if(!in_array($key, $keep_indexes)) { unset($data[0]['rowdata'][$key]; }
}

Edit, if it's not Key specific and it's just always the last 2 columns, try:

foreach($data[0]['rowdata'] as $key => $val) {
    $i = 1;
    $count = count($data[0]['rowdata'][$key]);
    foreach($data[0]['rowdata'][$key] as $key2 => $val2) {
        if($i >= ($count - 2)) { unset($data[0]['rowdata'][$key][$key2]); }
        $i++;
    }
}

1 Comment

no i cant make it key specific,i just want to remove last two key and their elemnet.

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.