Remove whitespace between array values

Asked

Viewed 9,533 times

0

Staff I have a array that 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?

  • 1

    There are several ways to do this Humberto, but treating this output at source may be better. Explain better where this array comes from?

3 answers

4


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

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

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

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

This will remove all whitespace between values as you wish.

An example of use 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++;

}

1

The best way to do this 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 did a functional example on Ideone: http://ideone.com/zHl3Ht

  • Thanks staff I thank you so much help from you and served me every example referred to here..

  • If the answers were helpful please vote in favour of them and accept the one you find most useful. Grateful.

0

You can use the str_replace, as already suggested. You did not specify, 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.