从数组中获取多个值

时间:2017-10-10 08:26:00

标签: php arrays explode

希望我能正确解释一下。

我的代码

$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)); 

然而,在DB中,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&amp; 3(1:3)

希望你能明白我的意思

谢谢你们

1 个答案:

答案 0 :(得分:1)

您应该回显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循环将从第二次迭代开始。