php - 使用换行符输出多个数组数据
问题描述
我正在做一个项目,我想用 Order Item Products 显示 Order Item Quantity。这是我的功能
<?php
function wpallexport_order_items($value) {
$order = wc_get_order($value);
foreach ( $order->get_items() as $item ) {
$qty[] = $item->get_quantity();
$name[] = $item->get_name();
$q = implode($qty);
$n = implode($name);
$output = $q .' * '. $n .'<br>';
}
return $output;
}
?>
但它给出的输出格式是
158 * Macroni Pasta Honey
我想要像这样的输出
1 * Macroni
5 * Pasta
8 * Honey
如何获得所需的输出?问候
解决方案
$data = [];
foreach ( $order->get_items() as $item ) {
// Collect all strings to one array
$data[] = $item->get_quantity() .' * '. $item->get_name();
}
// Then implode this array with `<br>` as glue
$output = implode('<br>', $data);
return $output;
推荐阅读
- graphviz - 如何在graphviz中左对齐图形
- r - Using tapply across different permutations of columns
- java - 如何使用java将数据插入数据类型为DBCLOB的数据库列?
- angular - 如何禁用插值字符串角度 2+
- android - 作为类的状态是否需要 SatefulWidget?
- python - What method does os.listdir() use to obtain a list of files in a directory?
- android - Http stream does not work on Android webview
- prometheus - How to display values of metrics in alert rules prometheus
- java - 如何使用 callUDF?
- mysql - Why is record missing while using limit and offset in mysql?