Home > Back-end >  Remove some keys from an array inside another array in PHP (Laravel)
Remove some keys from an array inside another array in PHP (Laravel)

Time:10-01

How to remove some keys from an array inside another array in PHP? I have this structure:

array (
  0 => 
  array (
    'num' => '123',
    'nome' => 'test 001'
    'pontos' => 68,
    'data_status' => '03/09/2021 10:05',
    'uuid_status' => '69450ea451ae11ea85ca309c23d3a0ed'
  ),
  1 => 
  array (
    'num' => '345',
    'nome' => 'test 002'
    'pontos' => 120,
    'data_status' => '27/08/2021 15:46',
    'uuid_status' => '3cbf4fd15d5411ea86956eef5d66cb13',
  ),
)

and need to return something like this:

array (
  0 => 
  array (
    'num' => '123',
    'nome' => 'test 001'
    'pontos' => 68
  ),
  1 => 
  array (
    'num' => '345',
    'nome' => 'test 002'
    'pontos' => 120
  )
)

I've seen some answers but they seem to be outdated, also i'm using Laravel, so it would help if someone point me out something from the framework and its equivalent in pure PHP

CodePudding user response:

A simple foreach loop is a good starting point, note this will remove items from the original array, by using the reference & to the array rather than a normal simple copy. On the $val in the foreach ( $input as &$val){

$input = [
    [   
        'num' => '123',
        'nome' => 'test 001',
        'pontos' => 68,
        'data_status' => '03/09/2021 10:05',
        'uuid_status' => '69450ea451ae11ea85ca309c23d3a0ed'
    ],[
        'num' => '345',
        'nome' => 'test 002',
        'pontos' => 120,
        'data_status' => '27/08/2021 15:46',
        'uuid_status' => '3cbf4fd15d5411ea86956eef5d66cb13'
    ]
];

$remove = ['data_status','uuid_status'];

foreach ( $input as &$val){
    foreach ($val as $k => $v) {
        if ( in_array($k,$remove) ) {
            unset( $val[$k]);
        }
    }
}
print_r($input);

RESULT

Array
(
    [0] => Array
        (
            [num] => 123
            [nome] => test 001
            [pontos] => 68
        )

    [1] => Array
        (
            [num] => 345
            [nome] => test 002
            [pontos] => 120
        )

)

CodePudding user response:

Probably not the most concise, but it gets the job done:

$bad_keys = array('data_status', 'uuid_status');




$array = array (
  0 => 
  array (
    'num' => '123',
    'nome' => 'test 001',
    'pontos' => 68,
    'data_status' => '03/09/2021 10:05',
    'uuid_status' => '69450ea451ae11ea85ca309c23d3a0ed',
  ),
  1 => 
  array (
    'num' => '345',
    'nome' => 'test 002',
    'pontos' => 120,
    'data_status' => '27/08/2021 15:46',
    'uuid_status' => '3cbf4fd15d5411ea86956eef5d66cb13',
  ),
);


function traverse_array($array, $bad_keys) {


foreach ($array as $key => $value) {
        
            if (is_array($value)) {
            
                    $array[$key] = traverse_array($value, $bad_keys);
            
            } else {
            
                    foreach ($bad_keys as $remove_me) {
            
                            if ($key == $remove_me) {
                
                                unset($array[$key]);
                    
                                }
                    }
        }
        
     }
     
     return $array;
}

print_r(traverse_array($array, $bad_keys));
  • Related