Note that there are some explanatory texts on larger screens.

plurals
  1. POI need to remove duplicates from an array from a specific code
    text
    copied!<p>I have the following code:</p> <pre><code>public function ajax() { // Contains results $data = array(); if( isset($this-&gt;request-&gt;get['keyword']) ) { // Parse all keywords to lowercase $keywords = strtolower( $this-&gt;request-&gt;get['keyword'] ); // Perform search only if we have some keywords if( strlen($keywords) &gt;= 3 ) { $parts = explode( ' ', $keywords ); $add = ''; // Generating search foreach( $parts as $part ) { $add .= ' AND (LOWER(pd.name) LIKE "%' . $this-&gt;db-&gt;escape($part) . '%"'; $add .= ' OR LOWER(p.model) LIKE "%' . $this-&gt;db-&gt;escape($part) . '%")'; } $add = substr( $add, 4 ); $sql = 'SELECT pd.product_id, pd.name, p.model FROM ' . DB_PREFIX . 'product_description AS pd '; $sql .= 'LEFT JOIN ' . DB_PREFIX . 'product AS p ON p.product_id = pd.product_id '; $sql .= 'LEFT JOIN ' . DB_PREFIX . 'product_to_store AS p2s ON p2s.product_id = pd.product_id '; $sql .= 'WHERE ' . $add . ' AND p.status = 1 '; $sql .= ' AND p2s.store_id = ' . (int)$this-&gt;config-&gt;get('config_store_id'); $sql .= ' ORDER BY LOWER(pd.name) ASC, LOWER(p.model) ASC'; $sql .= ' LIMIT 15'; $res = $this-&gt;db-&gt;query( $sql ); if( $res ) { $data = ( isset($res-&gt;rows) ) ? $res-&gt;rows : $res-&gt;row; // For the seo url stuff $basehref = 'product/product&amp;keyword=' . $this-&gt;request-&gt;get['keyword'] . '&amp;product_id='; foreach( $data as $key =&gt; $values ) { $data[$key] = array( 'name' =&gt; htmlspecialchars_decode($values['name'] . ' (' . $values['model'] . ')', ENT_QUOTES), 'href' =&gt; $this-&gt;url-&gt;link($basehref . $values['product_id']) ); } } } } echo json_encode( $data ); } </code></pre> <p>So, the array generates a list of products, like for e.g.:</p> <p><strong>Apple MacBook (Product Model 10)</strong></p> <p><strong>Apple МакБук (Product Model 10)</strong></p> <p>The problem is that those two products is actually one and the same product (same <strong>product_id</strong>) but in different languages, and both have the same URL.</p> <p>So, what I want to check is, while making the <strong>array</strong>, the code to check if there is already a product with that <strong>product_id</strong> in the array, and if there is, not to add another one with the same <strong>product_id</strong>.</p> <p>Practically, I don't want the <strong>array</strong> to generate two or more products with the same <strong>product_id</strong>.</p> <p><strong>EDIT:</strong> With Marc's code and ghbarratt suggestion work like a charm. A million thanks to you guys, and to all of you here.</p> <p><strong>P.S.</strong> How can I add ASC or DESC for <strong>ORDER BY pd.language_id</strong>:</p> <pre><code>$sql .= ' ORDER BY pd.language_id = ' . (int)$this-&gt;config-&gt;get('config_language_id'); $sql .= ' , LOWER(pd.name) ASC, LOWER(p.model) ASC'; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload