php - 循环遍历多维数组,拆分并将结果写入文件
问题描述
我正在尝试根据存储在 MySQL 数据库中的数据生成单独的文档。
表结构:
+----+-------+-----+------+-------+
| id | order | qty | item | total |
+----+-------+-----+------+-------+
| 1 | 1 | 1 | AAAA | 10 |
| 2 | 2 | 1 | AAAA | 20 |
| 3 | 2 | 1 | BBBB | |
| 4 | 2 | 1 | EEEE | |
+----+-------+-----+------+-------+
注意:列总计的值仅在“父”行上
所需的输出如下所示。
对于单行结果,一个 XML 文件包含:
<Order>1</Order>
<Total>10</Total>
<Detail>
<Qty>1</Qty>
<Item>AAAA</Item>
</Detail>
对于多行结果,一个(单独的)文件包含:
<Order>2</Order>
<Total>20</Total>
<Detail>
<Qty>1</Qty>
<Item>AAAA</Item>
</Detail>
<Detail>
<Qty>1</Qty>
<Item>BBBB</Item>
</Detail>
<Detail>
<Qty>1</Qty>
<Item>EEEE</Item>
</Detail>
我做的第一件事是获取多维数组中的数据:
$ordersArray = array();
$query = "SELECT * FROM `table`";
$getOrders = mysqli_query($db,$query);
$rows = mysqli_fetch_all($getOrders, MYSQLI_ASSOC);
foreach ($rows as $value) {
$ordersArray[$value['order']][] = $value;
}
或相当于:
$ordersArray = array(
1 => array(array('id' => '1', 'order' => '1', 'qty' => '1', 'item' => 'AAAA', 'total' => '10')),
2 => array(array('id' => '2', 'order' => '2', 'qty' => '1', 'item' => 'BBBB', 'total' => '20'), array('id' => '3', 'order' => '2', 'qty' => '1', 'item' => 'CCCCC', 'total' => ''), array('id' => '4', 'order' => '2', 'qty' => '1', 'item' => 'EEEE', 'total' => '')),
);
我设法输出所需的文件,只要它们只包含一行,使用:
foreach($ordersArray as $key => $value) {
foreach($value as $subkey => $subvalue) {
foreach($subvalue as $k => $v) {
$fileName = $subvalue['order'].".txt";
$output = '';
$output .= "<Order>".$subvalue['order']."</Order>\n";
$output .= "<Total>".$subvalue['total']."</Total>\n";
$output .= "<Details>\n";
$output .= "<Item>".$subvalue['item']."</Item>\n";
$output .= "<Quantity>".$subvalue['qty']."</Quantity>\n";
$output .= "</Details>\n";
}
file_put_contents($fileName, $output);
}
}
输出:
<Order>1</Order>
<Total>10</Total>
<Details>
<ProductSKU>AAAAA<ProductSKU>
<Quantity>1<Quantity>
</Details>
但是当一个订单有多行时,它只输出一个条目。 我应该怎么做才能确保链接到特定订单的所有项目都被分组并写入同一个文件?
解决方案
您有一个 for 循环来处理很多代码,并且您需要一个标记来仅获取第一个订单和总计。您添加的标签 Order1 以获得有效的 xml 代码。
<?php
$ordersArray = array(
1 => array(array('id' => '1', 'order' => '1', 'qty' => '1', 'item' => 'AAAA', 'total' => '10')),
2 => array(array('id' => '2', 'order' => '2', 'qty' => '1', 'item' => 'BBBB', 'total' => '20'), array('id' => '3', 'order' => '2', 'qty' => '1', 'item' => 'CCCCC', 'total' => ''), array('id' => '4', 'order' => '2', 'qty' => '1', 'item' => 'EEEE', 'total' => '')),
);
$output = '';
foreach($ordersArray as $key => $value) {
$output .= "<Order1>";
$n = 0;
$output = '';
foreach($value as $subkey => $subvalue) {
$fileName = $subvalue['order'].".txt";
IF($n == 0 )
{
$n = 1;
$output .= "<Order>".$subvalue['order']."</Order>\n";
$output .= "<Total>".$subvalue['total']."</Total>\n";
}
$output .= "<Details>\n";
$output .= "<Item>".$subvalue['item']."</Item>\n";
$output .= "<Quantity>".$subvalue['qty']."</Quantity>\n";
$output .= "</Details>\n";
}
$output .= "<Order1>";
file_put_contents($fileName, $output);
}
new dBug($output);
?>
推荐阅读
- amazon-web-services - 使用 FFmpeg 从受保护的 m3u8 直播流中获取屏幕截图
- flutter - 从静态区域获取 ThemeData
- c# - asp.net core: What is the best practice to execute code in a container application for kubernetes
- javascript - React Component from AJAX call
- xml - 如何使用 scala 从数据框创建 XML 字符串
- swift - XCTest 异步函数 Swift
- c# - how to only transform.position the x position and not also the y c# unity
- sql - Update remaining amount on a debit card with MS Access / SQL according to card number
- c++ - 将定义的类声明为模板参数内的结构
- r - 在多个数据帧上应用 ddply