PHP - Create an associative database array

Asked

Viewed 79 times

0

I have the information below in a bank:

inserir a descrição da imagem aqui

And I want the result in an array like this:

['CLARO' => "IPHONE 8", "VIVO" => "MOTO ONE", "TIM" => "ZENFONE 6", "CLARO" => "IPHONE 8", "CLARO" => GALAXY S9", "TIM" => "MOTO ONE Z", "TIM" => "MOTO ONE Z", "VIVO" => "MOTO ONE" e "VIVO" => "GALAXY S10"]

So far it has not worked very well (kkkkkkk) and I am with the following code:

$t_op  = array();
$t_mod = array();

$t_result = mysqli_query($conn, "SELECT operadora, modelo FROM sdc_tm_aparelhos WHERE id <> 1");
while ($t_row = mysqli_fetch_assoc($t_result)) {    
    $t_op[]  = $t_row['operadora'];
    $t_mod[] = $t_row['modelo'];
}
foreach ($t_op as $um) {
    foreach ($t_mod as $dois) {
        $novo = array($um=>$dois);
    }
}
print_r($novo);

This code presents me the following on the screen:

inserir a descrição da imagem aqui

I looked for solution here on the site but I could not find exactly what I need, if someone knows to answer or indicate some other topic very close to what I need!

  • 1

    But if you have two with TIM, what happens? It will not work, it is associative, each key represents only one, unless each value is a sub-array, that is, an array with more than one dimension.

  • The @Augustovasques response hasn’t worked yet, spawned a giant multimensional array...

  • I spelled it wrong. Change the last two foreach for array_combine($t_op, $t_mod);. But it will run what @Guilhermenascimento commented is an associative array and duplicate keys will be overwritten.

  • And is there any other way to do this without overwriting the keys even if it is in a way other than associative array? I have tried using array_combine and even array_merge and array_merge_recursive but none worked...

  • I thought about another way out, but I don’t know how to do it, take the table ID as well and use it as a key, because then it would be unique. The question is whether you can make an associative array with 3 elements (I think it only gives multidimensional, right?) or fall back on the question of the multidimensional array but then you wouldn’t need to take the ID...

  • 1

    What you can do is an array whose keys are operator names and each value is an array containing the models.

  • But why are there equal models for the same operator? For example, there is "moto one z" twice in "tim". You really need to duplicate the model in the array?

  • Yes because in the table each device has a different IMEI and also each operator can provide a multitude of devices. I’m trying to put together a multi array but I’m catching...

Show 3 more comments

1 answer

2


There is no way to create associative array with duplicate key values, they are unique.

What you can do is create a multidimensional associative array where key values would be in that structure:

inserir a descrição da imagem aqui

Note that the structure is an array of the carrier with the models, and in the models another array with the id’s (which you can replace with another database data).

The code would look like this:

$novo  = array();

$t_result = mysqli_query($conn, "SELECT id, operadora, modelo FROM sdc_tm_aparelhos WHERE id <> 1");
while ($t_row = mysqli_fetch_assoc($t_result)) {
   $novo[$t_row['item']][$t_row['nivel']][] = $t_row['id'];
}

print_r($novo);

Browser other questions tagged

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