Skip to main content
removeu 51 caracteres do conteúdo
Fonte Link
Duque
  • 646
  • 4
  • 10
$data  = array (
    array
        (
            0 => 'DDF',
            2 => '00001778BRASILIA',
            25 => '00032433NORTE (AGUAS CLARAS)',
            44 => '00000000',
            68 => 'RUA',
            102 =>   '00655588MANACA',
            124 => 'R MANACA',
            145 =>  '71907270NS',
            179 => ''
        ),
    array
        (
            0 => 'DDF',
            2 => '00001778BRASILIA',
            25 => '00052443RESIDENCIAL FLAMBOYANT (PLANALTINA)',
            39 => '00000000',
            63 => 'CONJUNTO',
            96 => '00742211A',
            119 =>   'CJ A',
            141 =>  '73366243NS',
            142 => ''

        ),
    array
        (
          0 => 'DDF',
          2 => '00001778BRASILIA',
          25 => '00032434SUL (AGUAS CLARAS)',
          44 =>   '00000000',
          68 => 'RUA',
          102 =>   '00575809MANACA',
          124 => 'R MANACA',
          145 =>  '71936500NS'
        )   );

    $newArray  =  array();

    $i  =  0;

    foreach ($data as $key => $value) {

            $newArray[$i]['cidade']      =   preg_replace("'/[^A-Za-z0[0-9?![:space:]]9]+/"',"", $value[2] );

            $newArray[$i]['bairro']      =   preg_replace("'/[^A-Za-z0[0-9?![:space:]]9]+/"',"", $value[25] );

            $newArray[$i]['logradouro']  =   $value[68] . ' ' . preg_replace("'/[^A-Za-z0[0-9?![:space:]]9]+/"',"", $value[102] );;

            $newArray[$i]['cep']         =   preg_replace("/\D/","", $value[145] );

            $i++;

    }

    print_r( $newArray );
$data  = array (
    array
        (
            0 => 'DDF',
            2 => '00001778BRASILIA',
            25 => '00032433NORTE (AGUAS CLARAS)',
            44 => '00000000',
            68 => 'RUA',
            102 =>   '00655588MANACA',
            124 => 'R MANACA',
            145 =>  '71907270NS',
            179 => ''
        ),
    array
        (
            0 => 'DDF',
            2 => '00001778BRASILIA',
            25 => '00052443RESIDENCIAL FLAMBOYANT (PLANALTINA)',
            39 => '00000000',
            63 => 'CONJUNTO',
            96 => '00742211A',
            119 =>   'CJ A',
            141 =>  '73366243NS',
            142 => ''

        ),
    array
        (
          0 => 'DDF',
          2 => '00001778BRASILIA',
          25 => '00032434SUL (AGUAS CLARAS)',
          44 =>   '00000000',
          68 => 'RUA',
          102 =>   '00575809MANACA',
          124 => 'R MANACA',
          145 =>  '71936500NS'
        )   );

    $newArray  =  array();

    $i  =  0;

    foreach ($data as $key => $value) {

            $newArray[$i]['cidade']      =   preg_replace("/[^A-Za-z0-9?![:space:]]/","", $value[2] );

            $newArray[$i]['bairro']      =   preg_replace("/[^A-Za-z0-9?![:space:]]/","", $value[25] );

            $newArray[$i]['logradouro']  =   $value[68] . ' ' . preg_replace("/[^A-Za-z0-9?![:space:]]/","", $value[102] );;

            $newArray[$i]['cep']         =   preg_replace("/\D/","", $value[145] );

            $i++;

    }

    print_r( $newArray );
$data  = array (
    array
        (
            0 => 'DDF',
            2 => '00001778BRASILIA',
            25 => '00032433NORTE (AGUAS CLARAS)',
            44 => '00000000',
            68 => 'RUA',
            102 =>   '00655588MANACA',
            124 => 'R MANACA',
            145 =>  '71907270NS',
            179 => ''
        ),
    array
        (
            0 => 'DDF',
            2 => '00001778BRASILIA',
            25 => '00052443RESIDENCIAL FLAMBOYANT (PLANALTINA)',
            39 => '00000000',
            63 => 'CONJUNTO',
            96 => '00742211A',
            119 =>   'CJ A',
            141 =>  '73366243NS',
            142 => ''

        ),
    array
        (
          0 => 'DDF',
          2 => '00001778BRASILIA',
          25 => '00032434SUL (AGUAS CLARAS)',
          44 =>   '00000000',
          68 => 'RUA',
          102 =>   '00575809MANACA',
          124 => 'R MANACA',
          145 =>  '71936500NS'
        )   );

    $newArray  =  array();

    $i  =  0;

    foreach ($data as $key => $value) {

            $newArray[$i]['cidade']      =   preg_replace('/[0-9]+/',"", $value[2] );

            $newArray[$i]['bairro']      =   preg_replace('/[0-9]+/',"", $value[25] );

            $newArray[$i]['logradouro']  =   $value[68] . ' ' . preg_replace('/[0-9]+/',"", $value[102] );;

            $newArray[$i]['cep']         =   preg_replace("/\D/","", $value[145] );

            $i++;

    }

    print_r( $newArray );
Fonte Link
Duque
  • 646
  • 4
  • 10

Amigo, desenvolvi sua solução, porém precisa tratar os erros que vão surgir, pois notei que os índices não são comuns. Fica por sua conta tratar essas exceções.

$data  = array (
    array
        (
            0 => 'DDF',
            2 => '00001778BRASILIA',
            25 => '00032433NORTE (AGUAS CLARAS)',
            44 => '00000000',
            68 => 'RUA',
            102 =>   '00655588MANACA',
            124 => 'R MANACA',
            145 =>  '71907270NS',
            179 => ''
        ),
    array
        (
            0 => 'DDF',
            2 => '00001778BRASILIA',
            25 => '00052443RESIDENCIAL FLAMBOYANT (PLANALTINA)',
            39 => '00000000',
            63 => 'CONJUNTO',
            96 => '00742211A',
            119 =>   'CJ A',
            141 =>  '73366243NS',
            142 => ''

        ),
    array
        (
          0 => 'DDF',
          2 => '00001778BRASILIA',
          25 => '00032434SUL (AGUAS CLARAS)',
          44 =>   '00000000',
          68 => 'RUA',
          102 =>   '00575809MANACA',
          124 => 'R MANACA',
          145 =>  '71936500NS'
        )   );

    $newArray  =  array();

    $i  =  0;

    foreach ($data as $key => $value) {

            $newArray[$i]['cidade']      =   preg_replace("/[^A-Za-z0-9?![:space:]]/","", $value[2] );

            $newArray[$i]['bairro']      =   preg_replace("/[^A-Za-z0-9?![:space:]]/","", $value[25] );

            $newArray[$i]['logradouro']  =   $value[68] . ' ' . preg_replace("/[^A-Za-z0-9?![:space:]]/","", $value[102] );;

            $newArray[$i]['cep']         =   preg_replace("/\D/","", $value[145] );

            $i++;

    }

    print_r( $newArray );