Change Array Structure in PHP

Asked

Viewed 20 times

-1

I get an Array that has the structure below, and I would like to clean it by changing the values of the ex keys: [19741N], which are random values by increasing numerical values 0 => 99999999999999:

Array
(
    [19741N] => Array
        (
            [Codigo] => 19741N
            [TipoDoImovel] => RESIDENCIAL
            [TipoImovel] => 
            [Finalidade] => VENDA
            [Venda] => Nao
            [Dormitorio] => 3
            [Dormitorios] => 4
            [TotalBanheiros] => 3
            [Estacionamento] => Nao
            [EstacionamentoVagas] => 
            [AreaConstruida] => 316.37
            [AreaTotal] => 
            [Endereco] => CARLOS PARUCKER
            [Numero] => 281
            [Complemento] => 
            [Bairro] => Atiradores
            [Cidade] => Joinville
            [UF] => SC
            [CEP] => 89203170
            [ValorVenda] => 980000
            [ValorAluguel] => 
            [ValorIptu] => 
            [ValorCondominio] => 
        )

[1238NA] => Array
        (
            [Codigo] => 13719
            [TipoDoImovel] => RESIDENCIAL
            [TipoImovel] => 
            [Finalidade] => VENDA
            [Venda] => Nao
            [Dormitorio] => 2
            [Dormitorios] => 3
            [TotalBanheiros] => 4
            [Estacionamento] => Nao
            [EstacionamentoVagas] => 
            [AreaConstruida] => 118.62
            [AreaTotal] => 
            [Endereco] => ITAIOPOLIS
            [Numero] => 651
            [Complemento] => 33
            [Bairro] => Saguaçu
            [Cidade] => Joinville
            [UF] => SC
            [CEP] => 89221155
            [ValorVenda] => 280000
            [ValorAluguel] => 
            [ValorIptu] => 
            [ValorCondominio] => 280
        )
)

I want to eliminate the first level, in case the [19741N], and this value is random, I tried to make use of the array_column(), but it didn’t work out just returns array { } , for a value only works, but not for the whole structure above, to be equal to below:

Array
(
    [0] => array {
        [Codigo] => 19741N
        [TipoDoImovel] => RESIDENCIAL
        [TipoImovel] => 
        [Finalidade] => VENDA
        [Venda] => Nao
        [Dormitorio] => 3
        [Dormitorios] => 4
        [TotalBanheiros] => 3
        [Estacionamento] => Nao
        [EstacionamentoVagas] => 
        [AreaConstruida] => 316.37
        [AreaTotal] => 
        [Endereco] => CARLOS PARUCKER
        [Numero] => 281
        [Complemento] => 
        [Bairro] => Atiradores
        [Cidade] => Joinville
        [UF] => SC
        [CEP] => 89203170
        [ValorVenda] => 980000
        [ValorAluguel] => 
        [ValorIptu] => 
        [ValorCondominio] => 
    },
    [1] => array {
        [Codigo] => 13719
        [TipoDoImovel] => RESIDENCIAL
        [TipoImovel] => 
        [Finalidade] => VENDA
        [Venda] => Nao
        [Dormitorio] => 2
        [Dormitorios] => 3
        [TotalBanheiros] => 4
        [Estacionamento] => Nao
        [EstacionamentoVagas] => 
        [AreaConstruida] => 118.62
        [AreaTotal] => 
        [Endereco] => ITAIOPOLIS
        [Numero] => 651
        [Complemento] => 33
        [Bairro] => Saguaçu
        [Cidade] => Joinville
        [UF] => SC
        [CEP] => 89221155
        [ValorVenda] => 280000
        [ValorAluguel] => 
        [ValorIptu] => 
        [Descricao] => Amplo apartamento muito bem localizado, próximo ao Sesc , 3 dormitórios sendo 1 suíte , cozinha com móveis planejados- sacada-  2 vagas de garagem.
        [ValorCondominio] => 280
    }
  • 2

    See the function array_values

  • first, recorded $arr2 = array_values($arr); and done. thanks

  • @Andersoncarloswoss make an answer for him :)

  • 1

    @Cypherpotato No time, can do...

1 answer

2


You can use the array_values to create a array where you have only the values, and therefore the keys will be indexes and not keys:

$array = [
    "aaa" => 12581,
    "bbb" => 182,
    "ccc" => "valor"
];

echo var_dump(array_values($array));

Exit:

array(3) {
    [0]=> int(12581)
    [1]=> int(182)
    [2]=> string(5) "valor" 
}

Documentation of array_values.

  • 1

    Thank you! It had worked out and that’s just how I needed it.

Browser other questions tagged

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