在多维数组中搜索

时间:2014-04-21 19:15:36

标签: php opencart

在我的网站上,我有一个通过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'])
                        );
}

2 个答案:

答案 0 :(得分:1)

不完全干净,但不使用任何功能,简单快捷。使用model作为表格密钥

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

稍后您可以使用array_values

重新索引数组

答案 1 :(得分: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;

}