Extract values from an array and mount one or the other

Asked

Viewed 153 times

-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

        )
    )
  • The array comes from where? it is possible to rename keys in the setting?

  • the array comes from a txt file, result obtained with that echo code "<pre>"; print_r($narr); echo "</pre>";

  • What is the relation of the first as the second array? What is the criterion for using the information? What you asked, can be answered with: $array2[$indicey] = $array1[$indicex]. Explain your problem better.

  • I developed the solution for you, but I noticed that Indice 68 and 102 are not repeated in all array. So this generated an error in the routine I did, but if you want I put here for you and you handle the exceptions of the indexes.

  • can post Duke...

1 answer

2


Friend, I developed your solution, but you need to deal with the mistakes that will appear, because I noticed that the indexes are not common. It is up to you to deal with 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 );
  • The problem is that the ZIP code is not always 145, in one of them it is 141, as well as other values, so it would not yet be a 100% certain answer, but I will not be negative so it obviously appears to be correct for "some cases" and is correct for the example given by the author of the question, I will test it later to give +1 =]. But I don’t think you need preg_replace in all cases, the CEP may use the (int)$value[145], in this case, in others only the numbers are removed, so you can use preg_replace('/[0-9]+/', '', $value[xx]), I believe, it becomes more understandable.

  • The code is correct, the preg_replace/REGEX used is not correct. Replacing the preg_replace by informed in the above comment it works correctly. I suggest you correct using the method mentioned above or by another equivalent solution.

  • @Inkeliz, that’s what you told him, he’s gonna have to deal with those exceptions. preg_replace has several 'faces', however, it is up to the programmer to use, more knowledge is never enough, your option is also valid.

  • @Inkeliz the regex of preg_replace works yes, I tested and here it is working correctly, as a better understanding, I will change it to the way described by you, so we can help other users.

  • I got an error using this. It kept the numbers. See http://sandbox.onlinephpfunctions.com/code/bbdfe3cd77f53d559bcd35e7fa052d4c6f2d1649.

  • @Humberto, note that the regex of preg_replace has been changed. This way is better understandable.

  • @Inkeliz, notices is because some of the indexes are not common. This was mentioned in the question comment and in the answer header. I’ve already changed the preg_replace.

  • 1

    Sorry, I didn’t see that. But, I already gave +1.

  • Seen @Duque, it worked here, I used array_values to reset the Index, thanks for all your collaboration.

  • Humberto, it was nothing

Show 5 more comments

Browser other questions tagged

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