从数组中获取多个值

Getting Multiple Values From Array

希望我解释正确。

我的代码中有

$eng[] = "All";
$eng[] = "engine_Sigma";
$eng[] = "engine_K-Series";
$eng[] = "engine_Duratec";
$eng[] = "engine_Suzuki";
$eng[] = "engine_Vauxhall";
$eng[] = "engine_Crossflow";

// query your db
$itemsq = mysqli_query($con,"SELECT * FROM `crm`.`workshop-items` LIMIT 0,100");
$items = mysqli_fetch_assoc($itemsq);
// do/while? should prob just use a while loop
do {
    $enginetype = explode(":", $items['engineid']);

    foreach ($enginetype as $key) {
        echo "$items[wsiid] - $items[code] - $items[incvat] - $eng[$key] <br>
";
    }

} while ($items = mysqli_fetch_assoc($itemsq)); 

但是在数据库中,engineid 存储为 1:3:6 甚至 1:2:3:4:5:6 现在我想做的是如果有多个值并且它们已被拆分, 而不是它只是回显第一个项目,然后再次回显下一个 id 的数据 (像这个 WSIID - CODE INCVAT engine_K-Series )等等 我想做显示(WSIID - CODE - INCVAT - engine_Sigma engine_K-Series) 如果 $eng 是 1 & 3 ( 1:3)

希望你能明白我的意思

谢谢大家

您应该回显 foreach 循环之外的数据的第一部分,并且只回显 foreach 内的引擎类型:

<?php
$eng[] = "All";
$eng[] = "engine_Sigma";
$eng[] = "engine_K-Series";
$eng[] = "engine_Duratec";
$eng[] = "engine_Suzuki";
$eng[] = "engine_Vauxhall";
$eng[] = "engine_Crossflow";

// query your db
$itemsq = mysqli_query($con,"SELECT * FROM `crm`.`workshop-items` LIMIT 0,100");
//$items = mysqli_fetch_assoc($itemsq); # you should delete this, see my explanation below
// do/while? should prob just use a while loop
do {
    $enginetype = explode(":", $items['engineid']);

    echo "$items[wsiid] - $items[code] - $items[incvat] - ";
    foreach ($enginetype as $key) {
       echo "$eng[$key] ";
    }
    echo "<br />";

} while ($items = mysqli_fetch_assoc($itemsq)); 

您应该删除第一次执行的 $items = mysqli_fetch_assoc($itemsq);,否则您的 do...while 循环将在第二次迭代时开始。