Help with object array

Asked

Viewed 42 times

1

Is there a way to add, not 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, make a mistake

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

This is my for to traverse:

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 be difficult to remove a bank from the array and manually put the index in order all over again, as I have 40 banks to create (all the time a new bank comes in or one or more is removed), so later on it will be difficult to do maintenance.

  • From what I understand you’ll be removing and adding items, right? If so, have you ever thought of using a chained list instead of an array?

  • not thought to use, this array contains several tables that will change in time to time, on the front the person chooses the name of the database (put a generic value), after I send this name pro php, there I look for that name and return the table name of the database. I did this so user did not have access to database name.

  • From the description of your problem I think you could use a list instead of the array, it will solve the main problem you described: "in the future it will be difficult to remove a database from the array and manually put the index in order all over again". With a list you don’t have to worry about reordering the index, it does this automatically.

  • how do I do this in php? a list of objects? rsrs

1 answer

2


From what I understand, you should check if an Dice 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 Dice, you can do so:

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

Browser other questions tagged

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