Remove specific element in similar arrays within array

2

I need to get the status of the following associative array array:

array (size=2)
0 => 
array (size=9)
  'idnovedades' => string '1' (length=1)
  'idGarantias' => string '329' (length=3)
  'numeroOperacion' => string '329' (length=3)
  'saldoAdeudado' => string '10000.00' (length=8)
  'montoGarantia' => string '1000000.05' (length=10)
  'saldoVencido' => string '12132.11' (length=8)
  'diasMora' => string '132131' (length=6)
  'observaciones' => string '321321qawasdadasda' (length=18)
  'estado' => string '1' (length=1)
  1 => 
array (size=9)
  'idnovedades' => string '2' (length=1)
  'idGarantias' => string '329' (length=3)
  'numeroOperacion' => string '329' (length=3)
  'saldoAdeudado' => string '3.29' (length=4)
  'montoGarantia' => string '3.29' (length=4)
  'saldoVencido' => string '2.39' (length=4)
  'diasMora' => string '239' (length=3)
  'observaciones' => string '239assadasd' (length=11)
  'estado' => string '1' (length=1)

For this I used this code:

 foreach ($registros['data'] as $key => $value) {
            unset($registros['data'][$key]['estado']);
       }
       return $registros;

It works, it works, but I wanted to know if there is a PHP method that solves this in a more efficient way or if I leave it like this.

    
asked by Fer 29.03.2018 в 16:25
source

2 answers

1

Another solution could be using array_walk

Example:

array_walk($registros['data'], function(&$data) {
    unset($data['estado']);
});

Demo

    
answered by 29.03.2018 / 17:53
source
2

I think you can use the function array_map ( link ). It serves to browse the elements of an array and apply a function to all of them. Something like this:

$array_nuevo = array_map(function($v) {
    unset($v['estado']);
    return $v;
}, $array_original);
    
answered by 29.03.2018 в 17:37