0
I have the information below in a bank:
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:
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!
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.
– Guilherme Nascimento
The @Augustovasques response hasn’t worked yet, spawned a giant multimensional array...
– Gustavo
I spelled it wrong. Change the last two
foreach
forarray_combine($t_op, $t_mod);
. But it will run what @Guilhermenascimento commented is an associative array and duplicate keys will be overwritten.– Augusto Vasques
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...
– Gustavo
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...
– Gustavo
What you can do is an array whose keys are operator names and each value is an array containing the models.
– Augusto Vasques
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?
– Sam
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...
– Gustavo