简体   繁体   English

如何在foreach循环内划分数组结果?

[英]How to divide array results inside a foreach loop?

I have a simple Select query in WordPress PHP, that selects all results from a database table, and I print the results on a page using the basic foreach, shown below: 我在WordPress PHP中有一个简单的Select查询,它从数据库表中选择所有结果,然后使用基本的foreach将结果打印在页面上,如下所示:

$newest = $wpdb->get_results("SELECT * FROM wp_refundrequests WHERE state = 'Odottavat' ORDER BY
  request_date DESC", ARRAY_A);
  foreach ($newest as $row) {
   echo "Product names: " . $row['product_name'];
}

My "product_name" column has multiple product names however, since I have inserted them as an array earlier. 我的“ product_name”列具有多个产品名称,因为我之前已将它们作为数组插入。 Here is an example of the data I have in my table: 这是表中数据的示例:

在此处输入图片说明

But I would like to print all of these names on new lines, so the results would be something along the lines of: 但我想将所有这些名称打印在新行上,因此结果将类似于以下内容:

Product one 
Product two
Product three

..etc etc, is there a way to do this? ..etc等,有没有办法做到这一点? I tried to do this with some tags, but that didn't work. 我尝试使用一些标签来执行此操作,但这没有用。

try this way 尝试这种方式

<?php

    $items = "item1,item2,item3"; // $row['product_name']
    $item = explode(",", $items);
    echo $item[0];

?>

Try this one: 试试这个:

foreach ($newest as $row) {
    $products = explode('","', $row['product_name']);
    foreach ($products as $product) {
        $product = trim($product); // not necessary
        echo "Product name: " . $product . "</br>";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM