Help with array of objects

1

Is there a way to add, without being manually, an indexed array? For example:

$arrayNomeBDs[0] = array (
    "nome_tabela" => "TabelaDeNome1",
    "db-tb-name" => "table_name_bd1"
);
$arrayNomeBDs[1] = array (
    "nome_tabela" => "TabelaDeNome1",
    "db-table-name" => "table_name_bd2"
);

If I put in the next iteration as index = 3 , it will give error

$arrayNomeBDs[3] = array (
    "nome_tabela" => "TabelaDeNome1",
    "db-tb-name" => "table_name_bd2"
);

This is my for to go through:

will give error if I put index = 3 if I follow the previous example:

for($i = 0; $i<sizeof($arrayBDs);$i++){
    var_dump($arrayBDs[$i]['nome_tabela']);
    if($arrayBDs[$i]['nome_tabela'] === $BD_Escolhido){
        return $arrayBDs[$i]['db-tb-name'];
    }
}

In the future it will take work to remove a bank from the array and manually index the indexes all over again, as I have 40 banks to create (all the time a new bank enters or one or more is removed), then forward it will take work to do the maintenance.

    
asked by anonymous 13.07.2015 / 23:02

1 answer

2

From what I understand, you should check if an index exists before comparing it. Avoiding Error:

for($i = 0; $i<sizeof($arrayBDs);$i++){
    var_dump($arrayBDs[$i]['nome_tabela']);
    if(isset($arrayBDs[$i]['nome_tabela']) && $arrayBDs[$i]['nome_tabela'] === $BD_Escolhido){
        return $arrayBDs[$i]['db-tb-name'];
    }
}

If you want to create array's without setting an index, you can do this:

$arrayNomeBDs = array();
$arrayNomeBDs[] = array (
    "nome_tabela" => "TabelaDeNome1",
    "db-tb-name" => "table_name_bd1"
);
$arrayNomeBDs[] = array (
    "nome_tabela" => "TabelaDeNome1",
    "db-table-name" => "table_name_bd2"
);
    
13.07.2015 / 23:49