如何在其中组合 foreach 的数组结果



我正在尝试显示愿望清单中的所有产品,woocommerce插件。另外,我想使愿望清单可排序,因此我将其连接到数据库,如果具有给定唯一ID的行不存在,它将按类别无组织地显示所有产品。

如果有唯一的 ID,那么它应该显示那里的所有项目。现在,它显示$row如下所示:

Array ( 
[0] => 237 
[1] => 243 
[2] => 266 
) 
Array ( 
[0] => 237 
[1] => 243 
[2] => 266 
) 
Array ( 
[0] => 237 
[1] => 243 
[2] => 266 
)

由于 3 个产品属于同一类别,因此它一次显示所有 3 个产品。

<?php
// db conn
$mysqli = new mysqli(
"localhost",
"root",
"root",
"nest"
);
// new wishist
$wishlist = new WC_Wishlists_Wishlist( $_GET['wlid'] );
// get products from wishlist
$wishlist_items = WC_Wishlists_Wishlist_Item_Collection::get_items( $wishlist->id, true );
// counter - useless at the moment
$counter = 0;
// loop through all products
foreach ( $wishlist_items as $wishlist_item_key => $item ) {
$counter++;
// get product id
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $item['product_id'], $item, $wishlist_item_key );
// get categories
$cats = get_the_terms($product_id, 'product_cat');
// loop categories
foreach ($cats as $cat) {
// get slug
$product_cat = $cat->slug;
}
// wishlist id is set in the database as a primary key - unique.
$wlid_cats = $product_cat . $wishlist->id;
// query with results
$result = $mysqli->query("
SELECT list 
FROM sortable 
WHERE wlid_cats = '$wlid_cats' "
);
// results parsed into $row
$row = $result->fetch_array(MYSQLI_NUM);
if (isset($row)) {
// change string to array
$row = implode(' ', $row);
// split array item[0] into diff items.
$row = preg_split("/[s,]+/", $row);
}
// get product data
$_product   = wc_get_product( $item['data'] );
if ( $_product->exists() && $item['quantity'] > 0 ) {
$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $item ) : '', $item, $wishlist_item_key );
// $cate is a function parameter which asks for the category to be displayed
if  ($product_cat == $cate && empty($row)) {
?>
<!--Saved for Jquery reasons - POST to db-->
<p style="display: none" id="<?= $wishlist->id ?>"  class="<?= $wishlist->id ?> "><?= $cate ?></p>
<li class="product-thumbnail" type="<?= $cate ?>" id="product-<?= $product_id ?>">
<?php
// gets thumbnail and displays it 
$thumbnail = apply_filters('woocommerce_cart_item_thumbnail', $_product->get_image(), $item, $wishlist_item_key);
echo $wlid_cats;
if (!$product_permalink) {
echo $thumbnail;
} else {
printf('<a href="%s">%s</a>', esc_url($product_permalink), $thumbnail);
}
?>
</li>
<?php
} elseif (!empty($row)) {
// where I'm stuck
print_r($row);
}
}
} ?>

如何合并输出以仅获取一个数组而不是 3 个数组?

创建新的全局数组

$narray = array();

之后在你的状况下

elseif (!empty($row)) {
// where I'm stuck
$narray[] = $row; // add the array
}
$result = array_map("unserialize", array_unique(array_map("serialize", $narray)));
print_r($result);

我正在尝试在这里解决问题。 不回避此查询

$result = $mysqli->query("
SELECT list 
FROM sortable 
WHERE wlid_cats = '$wlid_cats' "
);

拥有类别列表而不仅仅是一个类别,然后如果有重复项,它将对其进行排序。

只是重新阅读您的问题并试图弄清楚这一切。

所以在第 12 行,你有项目的 ID,现在你需要按"可排序"表对它们进行排序吗?

所以查询(如果这只是普通的sql(将是这样的。

订单的 ID//itemId

$order = "itemId=3 DESC,itemId=1 DESC,itemId=5 DESC,itemId=6 DESC,category DESC";
$query =  "SELECT * FROM items WHERE itemID IN (myItems) ORDER BY ".$order;

也许我在这里错过了重点,但 sql 是合理的。

最新更新