2014-04-21 54 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)。這將很快並且使用最少的額外內存。 –