我试图理解为什么会得到如此不合逻辑的结果,这是2天。让我举个例子来解释一下。我根据这个简单的数组创建发票。
Array
(
[0] => Array
(
[userid] => 208
[number_of_services] => 3
[username1] => sara
[income1] => 10
[username2] => mark
[income2] => 18
[username3] => joe
[income3] => 12
)
[1] => Array
(
[userid] => 4
[number_of_services] => 1
[username1] => bruce
[income1] => 5
)
[2] => Array
(
[userid] => 303
[number_of_services] => 1
[username1] => michael
[income1] => 7
)
)
使用此数组我想为具有以下ID的客户创建3张发票: 208 , 4 , 303 。我可以用一个简单的foreach来实现它。
foreach($myarray AS $key => $value)
{
// create my invoice
}
现在您可以看到ID 208 的客户端有多个用户名。我将它们存储为[username1],[username2],[username3]等等。
[0] => Array
(
[userid] => 208
[number_of_services] => 3
[username1] => sara
[income1] => 10
[username2] => mark
[income2] => 18
[username3] => joe
[income3] => 12
)
我想在发票中将每个用户名显示为新行(项目),因此我执行以下操作:
foreach($myarray AS $key => $value)
{
for($n=1; $n<=$value["number_of_services"]; $n++)
{
$invoice["itemdescription$n"] = "Username: ".$value["username$n"]."";
$invoice["itemamount$n"] = $value["income$n"];
$invoice["itemtaxed$n"] = 1;
}
}
现在有了这个循环我应该:
但我总是得到:
我不明白为什么脚本会将客户 208 的[username2],[income2]和[username3],[income3]“合并”到客户端 4 和 303 !我在每一步调试脚本,问题似乎在for()循环中。
有什么想法吗?
答案 0 :(得分:0)
foreach($myarray AS $key => $value)
{
for($n=1; $n<=$value["number_of_services"]; $n++)
{
$invoice[] = array(
"itemdescription" => "Username: " . $value . "username" . $n,
"itemamount" . $n = $value . "income" . $n,
"itemtaxed" . $n = 1
);
}
}
试试这个。