我有以下嵌套循环。第一个得到manufacturers_name
,有时在该字段中有多个名称我正在使用第二个foreach循环来拆分数组并获取每个制造商的ID。问题是循环停在第一个元素$pieces
。
循环:
foreach ($items as $v) {
$getname = xtc_db_query("SELECT `manufacturers_name` FROM `manufacturers` WHERE `manufacturers_id`=(SELECT manufacturers_id FROM products WHERE products_id='".$v."')");
$getnameresult= xtc_db_fetch_array($getname);
$pieces = explode(";", $getnameresult[manufacturers_name]);
foreach ($pieces as $n)
{
$getid = xtc_db_query("SELECT `manufacturers_id` FROM `authors` WHERE `manufacturers_name` LIKE '".$n."' ");
$getidresult = xtc_db_fetch_array($getid);
echo $getidresult['manufacturers_id'];
echo $n."<br>";
}
}
结果:
2Achtzehn, Hans-Jürgen
2Achtzehn, Hans-Jürgen
2Achtzehn, Hans-Jürgen
2Achtzehn, Hans-Jürgen
2Achtzehn, Hans-Jürgen
2Achtzehn, Hans-Jürgen
228Weisenburger, Birgit
228Weisenburger, Birgit
2Achtzehn, Hans-Jürgen
33Müller, Ulrike
Schmitz, Irmgard
Hampel, Heike
Riedel, Sara
Achtzehn, Hans-Jürgen
Krüger, Andreas
Vogt, Melanie
预期结果:
2Achtzehn, Hans-Jürgen
2Achtzehn, Hans-Jürgen
2Achtzehn, Hans-Jürgen
2Achtzehn, Hans-Jürgen
2Achtzehn, Hans-Jürgen
2Achtzehn, Hans-Jürgen
228Weisenburger, Birgit
228Weisenburger, Birgit
2Achtzehn, Hans-Jürgen
33Müller, Ulrike
56Schmitz, Irmgard
77Hampel, Heike
75Riedel, Sara
34Achtzehn, Hans-Jürgen
65Krüger, Andreas
80Vogt, Melanie
答案 0 :(得分:0)
foreach ($items as $v) {
products_id='".$v."'");
更改为
foreach ($items as $v) {
答案 1 :(得分:0)
我做了一个var_dump($ pieces)和var_dump($ n)来解决这个问题,结果发现名字前面有一个空格,这就是为什么我没有得到他们的ID:
$ pieces -
array (size=8)
0 => string 'Müller, Ulrike' (length=14)
1 => string ' Schmitz, Irmgard' (length=17)
2 => string ' Hampel, Heike' (length=14)
...
代表$ n -
string 'Müller, Ulrike' (length=14)
string ' Schmitz, Irmgard' (length=17)
string ' Hampel, Heike' (length=14)
<强>修正:强>
将以下函数放入我的文档中,然后修剪循环:
function trim_value(&$value)
{
$value = trim($value);
}
在第二个foreach循环之前修剪$pieces
:
array_walk($pieces, 'trim_value');