use the trim () function within an array

2

It happens that I have a select where the options are generated from a query to a database. Sometimes there are repeated options because at the beginning or at the end of each option you have blank spaces. I have the following code where I try to apply the function trim () to an array but it generates the following error: ** Warning: trim () expects parameter 1 to be string, array given in C ** I was trying to do it this way :

if (isset($sistemas)) {
      trim($sistemas);
      $sistemas2 = array_unique($sistemas);
                            }
but I still do not know how to apply the trim to the array.     
asked by Edgar Yezid Cruz 19.04.2018 в 17:51
source

3 answers

1

You can use array_map, which iterates over each object and can receive 2 arguments, where the first is the function you want to apply and the second is the array you want to operate.

$lenguajes = array(  "     php", "     java", "  mysql");

$lenguajes_sin_espacios = array_map('trim', $lenguajes);

var_dump($lenguajes_sin_espacios);
    
answered by 19.04.2018 / 18:02
source
5

With this you should be able to solve your problem:

$frutas= array('  manzana','plantano  ', ' , ',     '            cereza ');
print_r($frutas);//pintamos las frutas en un inicio

$trimmed_array=array_map('trim',$frutas);

print_r($trimmed_array);//pintamos las frutas sin espacios

I hope I have served you!

    
answered by 19.04.2018 в 17:59
1

If you are going to work with a multidimensional array, the use of array_map would fail. I leave here a possibility using array_walk_recursive with which we can:

  

Apply a user function recursively to each member of a   array.

     

PHP Manual

If we have the following array:

$arrProductos = array(
                    array(
                            'id' => 1,
                            'Tipo' => "  Frutas  ",
                            'Nombres' => 
                                array('  manzana','plantano  ', ' , ',     '            cereza ')
                         ),
                    array(
                            'id' => 2,
                            'Tipo' => "Pescados",
                            'Nombres' => 
                                array('  Dorada','Atún  ', ' Merluza ',     
                                         'Salmón'=> array('   Ahumado', 'Normal   ')
                                     )
                        )
                    );

We can clean all its values with the following function:

array_walk_recursive($arrProductos,function(&$v){
    $v=trim($v); 
});

The result would be:

print_r($arrProductos);


Array
(
    [0] => Array
        (
            [id] => 1
            [Tipo] => Frutas
            [Nombres] => Array
                (
                    [0] => manzana
                    [1] => plantano
                    [2] => ,
                    [3] => cereza
                )

        )

    [1] => Array
        (
            [id] => 2
            [Tipo] => Pescados
            [Nombres] => Array
                (
                    [0] => Dorada
                    [1] => Atún
                    [2] => Merluza
                    [Salmón] => Array
                        (
                            [0] => Ahumado
                            [1] => Normal
                        )

                )

        )

)

Test code:

SEE DEMO IN REXTESTER

    
answered by 19.04.2018 в 19:43