根据我所知道的,您不能回荡阵列。您必须循环通过它。为什么我的数组值打印了?
我有这些数据:
1CPT5
'stock' => array (
'warehouse' =>
array (
'warehouse_id' => 1,
'name' => 'CPT',
),
'quantity_available' => 5,
)
3JHB21
'stock' = > array (
'warehouse' =>
array (
'warehouse_id' => 3,
'name' => 'JHB',
),
'quantity_available' => 21,
)
以及通过上述数据循环的代码。
foreach ($row['stock'] as $val) {
foreach ($val['warehouse'] as $warehouse => $name) {
echo $name;
}
echo $val['quantity_available'];
echo '<pre>' . var_export($val, true) . '</pre>';
}
我试图根据仓库的ID(有2个仓库)显示Quantity_available,第一组数据是针对名称CPT和项目5的Warehouse_ID 1。 第二组数据是针对具有名称JHB和项目数量21的Warehouse_ID 3。 “仓库”是一个数组,据我所知,您只能通过数组,print_r或var_dump循环一个数组。为什么如果我会回声$名称,我会看到数组的所有值,以及如何隔离它们,以便我可以获取ID并基于ID显示与该ID关联的数量?
我终于弄清楚了,以下代码显示了我所追求的数字:
foreach ($row['stock_at_takealot'] as $key => $val) {
foreach ($val['warehouse'] as $key => $value) {
if ($value == 1) {
echo $val['quantity_available'];
}
}
};
但是现在我还有另一个问题,其中所有值都在一个单元格中而不是两个显示。我在这里是什么错误?
<td class='small centerCol'>";
foreach ($row['stock_at_takealot'] as $key => $val) {
foreach ($val['warehouse'] as $key => $value) {
if ($value == 1) {
echo $val['quantity_available'];
}
}
};
"</td>
<td class='small centerCol'>";
foreach ($row['stock_at_takealot'] as $key => $val) {
foreach ($val['warehouse'] as $key => $value) {
if ($value == 3) {
echo $val['quantity_available'];
}
}
};
"</td>
<td class='small centerCol'>"
I have this data:
1CPT5
'stock' => array (
'warehouse' =>
array (
'warehouse_id' => 1,
'name' => 'CPT',
),
'quantity_available' => 5,
)
3JHB21
'stock' = > array (
'warehouse' =>
array (
'warehouse_id' => 3,
'name' => 'JHB',
),
'quantity_available' => 21,
)
and this code that is looping through the above data.
foreach ($row['stock'] as $val) {
foreach ($val['warehouse'] as $warehouse => $name) {
echo $name;
}
echo $val['quantity_available'];
echo '<pre>' . var_export($val, true) . '</pre>';
}
I am trying to display the quantity_available based on the ID of the warehouse (there are 2 warehouses) The first set of data is for warehouse_id 1 with name CPT and number of items 5.
The second set of data is for warehouse_id 3 with name JHB and number of items 21.
'warehouse' is an array and according to what I know you can only loop through an array, print_r or var_dump an array. Why if I then echo $name do I see all the values of the array and how do I isolate them so I can just get the id and based on the id display the quantity associated with that id?
I finally figured it out and the following code displays the number I am after:
foreach ($row['stock_at_takealot'] as $key => $val) {
foreach ($val['warehouse'] as $key => $value) {
if ($value == 1) {
echo $val['quantity_available'];
}
}
};
but now I have another issue where all the values are displayed in one cell instead of two. What is my mistake here please?
<td class='small centerCol'>";
foreach ($row['stock_at_takealot'] as $key => $val) {
foreach ($val['warehouse'] as $key => $value) {
if ($value == 1) {
echo $val['quantity_available'];
}
}
};
"</td>
<td class='small centerCol'>";
foreach ($row['stock_at_takealot'] as $key => $val) {
foreach ($val['warehouse'] as $key => $value) {
if ($value == 3) {
echo $val['quantity_available'];
}
}
};
"</td>
<td class='small centerCol'>"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论