Fernando Oliveira Fernando Oliveira - 6 months ago 17
SQL Question

How to combine data from two different tables into a variable

Can anyone tell me how I can compare a variable that stores a country with 2 tables in the database?

I have this data in the database:



country values
PT|AU|USA 0.03|0.04|0.05





And I have a variable that will store one of three values: EN, AU, USA
I would like this to get the data from the two tables but in the correct order.



PT => 0.03
AU => 0.04
USA => 0.05





I tried this way but it didn't work



$countries = $get1x["country"];
$values = $get1x["values"];

$tags = explode('|' , $countries);
$tags2 = explode('|' , $values);

$country = "";
$value = 0.0;

foreach($tags as $i => $key) {
$i > 0;
$value = str_split($key, $ii);
}
foreach($tags2 as $i2 => $key2) {
$i2 > 0;
$value = str_split($key2, $iii);
}

print_r($country);





Can anyone tell me a way?

Answer

You need explode and array_combine(), i.e:

$countries  = explode("|", "PT|AU|USA") ;
$values     = explode("|", "0.03|0.04|0.05") ;
$combined = array_combine($countries, $values);
print_r($combined);

Array
(
    [PT] => 0.03
    [AU] => 0.04
    [USA] => 0.05
)

Ideone Demo