Extract values from an array and mount one or other [closed]

-4
Array
(
    [0] => Array
        (
            [0] => DDF
            [2] => 00001778BRASILIA
            [25] => 00032433NORTE (AGUAS CLARAS)
            [44] => 00000000
            [68] => RUA
            [102] =>   00655588MANACA
            [124] => R MANACA
            [145] =>  71907270NS
            [179] =>  

        )

    [1] => Array
        (
            [0] => DDF
            [2] => 00001778BRASILIA
            [25] => 00052443RESIDENCIAL FLAMBOYANT (PLANALTINA)
            [39] => 00000000
            [63] => CONJUNTO
            [96] => 00742211A
            [119] =>   CJ A
            [141] =>  73366243NS
            [142] =>   

        )

    [2] => Array
        (
            [0] => DDF
            [2] => 00001778BRASILIA
            [25] => 00032434SUL (AGUAS CLARAS)
            [44] =>   00000000
            [68] => RUA
            [102] =>   00575809MANACA
            [124] => R MANACA
            [145] =>  71936500NS
        )

)

Expected result

Array(

  [0] => Array
        (
            ['cidade'] => BRASILIA
            ['bairro'] =>NORTE (AGUAS CLARAS)
            ['logradouro'] => RUA MANACA
            ['cep'] =>71907270

        )
    )
    
asked by anonymous 25.04.2016 / 19:29

1 answer

2

Friend, I've developed your solution, but you need to handle the errors that will arise, as I've noticed that indexes are not common. It is up to you to handle these exceptions.

$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 );
    
25.04.2016 / 20:42