首页 > 解决方案 > 使用换行符输出多个数组数据

问题描述

我正在做一个项目,我想用 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

如何获得所需的输出?问候

标签: phpexcelwordpressfunctionwoocommerce

解决方案


$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;

推荐阅读