使用php将单位与优势结合起来

使用php将单位与优势结合起来,php,mysql,Php,Mysql,假设我有一个药物数据库,其格式如下: ----------------------------------------------- | Medication Name | Strength | Unit | ----------------------------------------------- | NORCO | 5;325 | mg/1;mg/1 | | Amoxicillin | 500 | mg

假设我有一个药物数据库,其格式如下:

-----------------------------------------------
| Medication Name |  Strength  |    Unit      |
-----------------------------------------------
| NORCO           |   5;325    |  mg/1;mg/1   |
| Amoxicillin     |     500    |     mg/1     |
| Augmentin       |  250;62.5  |mg/5mL; mg/5mL|
-----------------------------------------------
如何使用php以这种方式显示数据:

NORCO 5mg/325mg
Amoxicillin 500mg
Augmentin 250mg/5mL 62.5mg/5mL

使用
str\u replace
可以很容易地从单位列中删除
/1
,但是如何使用分号将单位分配到强项?

您可以使用php
explode
函数。可能还有其他更好的解决方案,对于初学者,您可以尝试下面的代码

// Considering this is your data from database
$data = array(
            array(
                'name' => "NORCO",
                'strength' => "5;325",
                'unit' => "mg/1;mg/1"
            ),
            array(
                'name' => "Amoxicillin",
                'strength' => "500",
                'unit' => "mg/1"
            ),
            array(
                'name' => "Augmentin",
                'strength' => "250;62.5",
                'unit' => "mg/5mL; mg/5mL"
            ),
        );

// Looping through data
foreach ($data as $row) {
    $strength = explode(';', $row['strength']);
    $unit = explode(';', $row['unit']);
    $combine = combineStrengthUnit($strength, $unit);   
    echo $row['name'] . " " . $combine ;
    echo "<br/>";
}

// return combined Strength and Units
function combineStrengthUnit($strength, $unit)
{
    $combine_result = '';
    foreach ($strength as $key => $value) {
        $combine_result .= $value . trim(str_replace('/1', '', $unit[$key])) . " "; //trimming the spaces of unit
    }
    return $combine_result;
}

请展示您迄今为止所做的尝试。即使有人在此处向您提出查询,您也应该真正规范化您的数据库并删除那些分号分隔的列表。相信我,我会这样做的。。但政府没有。数据库的来源是美国食品和药物管理局(FDA)。资料来源:我并不感到惊讶,如果这个表格的作者被提升了好几次,我也不会感到惊讶。让我们看看如何开始思考。您可以在php或mysql查询中通过split函数拆分字符串。但是通过php它更简单。
NORCO 5mg 325mg
Amoxicillin 500mg
Augmentin 250mg/5mL 62.5mg/5mL