2014-04-21 97 views
0

在我的网站上,我有一个通过foreach生成的产品列表,并创建了一个多维数组。在多维数组中搜索

阵列是这样的:(短) http://k44.kn3.net/73194E0F9.jpg

我需要将此多维数组中进行搜索,如果的["model"]重复的值。

总之,我不想用相同的["model"]

阵列显示2个产品做定义:

foreach ($results as $result) { 
    $this->data['products'][] = array(
         'product_id' => $result['product_id'], 
         'model' => $corto, 
         'thumb'  => $image, 
         'Disponibilidad' => $rstock, 
         'name'  => $nombre, 
         'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 100) . '..', 
         'price'  => $price, 
         'special'  => $special, 
         'tax'   => $tax, 
         'rating'  => $result['rating'], 
         'reviews'  => sprintf($this->language->get('text_reviews'), (int)$result['reviews']), 
         'href'  => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id']) 
         ); 
} 
+0

403禁止 - 检查图像 – mleko

+0

@mleko这里工作得很好。 – 13ruce1337

+0

@ 13ruce1337现在也适用于我 – mleko

回答

1

不是完全干净,但容易和快速不使用任何功能。使用model如表密钥

$this->data['products'][$result['model']] = array(
         'product_id' => $result['product_id'], 

以后可以重新索引阵列array_values

1

阵列产品是变型产品。未经测试但应该可以工作:

$products // Your product array here 
$models = array(); 
$count = 0; 
foreach ($products as $product) { 
    // If Model Already exits (unset/remove from array) 
    if (in_array($product['model'], $models)) unset($products[$count]); 
    // Add Model Number to Array 
    array_push($models, $product['model']); 
    // Increase Count 
    $count++; 
} 

保持已添加模型的阵列,并且已添加未设置。

更新到你的更新代码:

$models = array(); 
foreach ($results as $result) { 

    // Check if Model exits 
    if (in_array($corto, $models)) continue; 

    $this->data['products'][] = array(
     'product_id' => $result['product_id'], 
     'model' => $corto, 
     'thumb'  => $image, 
     'Disponibilidad' => $rstock, 
     'name'  => $nombre, 
     'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 100) . '..', 
     'price'  => $price, 
     'special'  => $special, 
     'tax'   => $tax, 
     'rating'  => $result['rating'], 
     'reviews'  => sprintf($this->language->get('text_reviews'), (int)$result['reviews']), 
     'href'  => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id']) 
    ); 

    // Add Model to array 
    array_push($models, $corto); 

} 

更新由Ryan以提高性能:

$models = array(); 
foreach ($results as $result) { 

    // Check if Model exits 
    if (isset($models[$corto])) continue; 

    $this->data['products'][] = array(
     'product_id' => $result['product_id'], 
     'model' => $corto, 
     ..... 
     'href'  => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id']) 
    ); 

    // Add Model to array 
    $models[$corto] = true; 

} 
+0

不要使用'in_array',因为它对大数组来说会很慢。在模型查找数组中使用'model'作为'key',如下所示:$ models [$ product ['model']] = true ;.现在使用isset($ models [])进行查找测试。即使对于大型阵列也会保持不变的性能。 –

+0

Cheers Ryan我可以添加此更新 – ptimson

+0

如果您在foreach循环中添加对数组元素的引用,则可以随时取消设置重复项,如下所示:foreach($ results as&$ result)。这将很快并且使用最少的额外内存。 –