Remove whitespace between values in an array

0

I have a array which is returning the following data as shown in the following example:

Array
(
    [0] => Array
        (
            [0] => DDF      00001778BRASILIA                                                                     00052442VILA FELIZ (PLANALTINA)                                                      00000000                                                                        CONJUNTO                                                                                                   00742207A                                                                       CJ A                                                                    73366203NS                                                                                                        

        )

    [1] => Array
        (
            [0] => DDF      00001778BRASILIA                                                                     00052442VILA FELIZ (PLANALTINA)                                                      00000000                                                                        CONJUNTO                                                                                                   00742207A                                                                       CJ A                                                                    73366203NS                                                                                                        

            [1] => DDF      00001778BRASILIA                                                                     00052443RESIDENCIAL FLAMBOYANT (PLANALTINA)                                          00000000                                                                        CONJUNTO                                                                                                   00742211A                                                                       CJ A                                                                    73366243NS     

        )
)

How do I remove whitespace between values?

    
asked by anonymous 25.04.2016 / 01:45

3 answers

3

If you want to remove all spaces use str_replace() , see the manual .

 str_replace(' ', '', 'DDF      00001778BRASILIA');
 // Resultado: DDF00001778BRASILIA

If you want to remove only the repeated spaces, but keep one of them, use preg_replace.

 preg_replace('/\s+/', ' ', 'DDF      00001778BRASILIA');
 // Resultado: DDF 00001778BRASILIA

This will remove all the white space between the values, as desired.

An example usage for your case, in specific:

<?php


$arrays = array(
    array(
            'DDF      00001778BRASILIA                                                                     00052442VILA FELIZ (PLANALTINA)                                                      00000000                                                                        CONJUNTO                                                                                                   00742207A                                                                       CJ A                                                                    73366203NS'                                                                                                        

        ),

    array
        (
            'DDF      00001778BRASILIA                                                                     00052442VILA FELIZ (PLANALTINA)                                                      00000000                                                                        CONJUNTO                                                                                                   00742207A                                                                       CJ A                                                                    73366203NS',                                                                                                        

            'DDF      00001778BRASILIA                                                                     00052443RESIDENCIAL FLAMBOYANT (PLANALTINA)                                          00000000                                                                        CONJUNTO                                                                                                   00742211A                                                                       CJ A                                                                    73366243NS'     

        )
);

$idPrincipal = 0;
$idSecundario = 0;

foreach($arrays as $array){



   foreach($array as $a){
       # Remover todos:
       $arrays[$idPrincipal][$idSecundario] = str_replace(' ', '', $a);
       // OU
       # Remover repetidos, mantendo um:
       $arrays[$idPrincipal][$idSecundario] = preg_replace('/\s+/', ' ', $a);

       $idSecundario++;
   }

   $idPrincipal++;

}
    
25.04.2016 / 02:21
0

You can use str_replace , as already suggested. You did not specify it, but I'm pretty sure you just want to eliminate the extra spaces.

So I suggest you do it this way:

  $array = [
    ["DDF      00001778BRASILIA                                                                     00052442VILA FELIZ (PLANALTINA) "],
    ["DDF      00001778BRASILIA                                                                     00052442VILA FELIZ (PLANALTINA)",
     "DDF      00001778BRASILIA                                                                     00052443RESIDENCIAL FLAMBOYANT (PLANALTINA)"
    ]
  ];


for ($i=0;$i<count($array);$i++){
      for ($j=0;$j<count($array[$i]);$j++){
          $array[$i][$j] = trim(preg_replace('/ +/',' ', $array[$i][$j]));
      }
  }


  print_r($array);
    
25.04.2016 / 02:35
0

The best way to do this in my view is to use the array_map function. So:

function removeEspacos($str) {
    return str_replace(' ', '', $str);
}; 

$narr = array();
foreach($arr as $a)
    $narr[] = array_map('removeEspacos', $a);

I made a functional example in Ideone: link

    
25.04.2016 / 05:14