php - How to combine array results of foreach inside of it -
i trying display products wishlist, woocommerce plugin. also, want make wishlist sortable connect db , if row given unique id not there, display products category unorganised.
if there unique id should display items there. right displays $row following:
array ( [0] => 237 [1] => 243 [2] => 266 ) array ( [0] => 237 [1] => 243 [2] => 266 ) array ( [0] => 237 [1] => 243 [2] => 266 )
due 3 products being in same category, displays them 3 @ once.
<?php // db conn $mysqli = new mysqli( "localhost", "root", "root", "nest" ); // new wishist $wishlist = new wc_wishlists_wishlist( $_get['wlid'] ); // products wishlist $wishlist_items = wc_wishlists_wishlist_item_collection::get_items( $wishlist->id, true ); // counter - useless @ moment $counter = 0; // loop through products foreach ( $wishlist_items $wishlist_item_key => $item ) { $counter++; // product id $product_id = apply_filters( 'woocommerce_cart_item_product_id', $item['product_id'], $item, $wishlist_item_key ); // categories $cats = get_the_terms($product_id, 'product_cat'); // loop categories foreach ($cats $cat) { // slug $product_cat = $cat->slug; } // wishlist id set in database primary key - unique. $wlid_cats = $product_cat . $wishlist->id; // query results $result = $mysqli->query(" select list sortable wlid_cats = '$wlid_cats' " ); // results parsed $row $row = $result->fetch_array(mysqli_num); if (isset($row)) { // change string array $row = implode(' ', $row); // split array item[0] diff items. $row = preg_split("/[\s,]+/", $row); } // 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 function parameter asks category displayed if ($product_cat == $cate && empty($row)) { ?> <!--saved jquery reasons - post 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 , displays $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)) { // i'm stuck print_r($row); } } } ?>
how can merge output 1 array instead of 3?
make new global array
$narray = array();
and after in condition
elseif (!empty($row)) { // i'm stuck $narray[] = $row; // add array } $result = array_map("unserialize", array_unique(array_map("serialize", $narray))); print_r($result);
Comments
Post a Comment