Php 多维数组中的搜索
在我的网站上,我有一个通过Php 多维数组中的搜索,php,opencart,Php,Opencart,在我的网站上,我有一个通过foreach生成的产品列表,并创建了一个多维数组 数组如下:(短) 如果[“model”]的值重复,我需要在这个多维数组中搜索 简而言之,我不想显示具有相同[“model”] 数组定义: foreach ($results as $result) { $this->data['products'][] = array( 'product_id' => $result['product_id'],
foreach
生成的产品列表,并创建了一个多维数组
数组如下:(短)
如果[“model”]
的值重复,我需要在这个多维数组中搜索
简而言之,我不想显示具有相同[“model”]
数组定义:
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'])
);
}
不是很干净,但是简单快捷,不需要使用任何功能。使用
model
作为表键
$this->data['products'][$result['model']] = array(
'product_id' => $result['product_id'],
稍后您可以使用
数组\u值重新索引数组
产品数组是变量产品。未经测试但应有效:
$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);
}
$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;
}
Ryan更新以提高性能:
$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);
}
$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;
}
403禁止-检查image@mleko在这里可以正常工作。@13ruce137现在适合我,让我们看看代码,而不是它的图像。同时发布您迄今为止尝试过的内容。不要使用“in_array”,因为它对于大型阵列来说会很慢。使用“模型”作为模型查找数组中的“键”,如:$models[$product['model']]=true;中所示;。现在使用'isset($models[])进行查找测试。即使对于大型数组,也将提供恒定的性能。如果在foreach循环中添加对数组元素的引用,则可以在执行时取消设置重复项,如:foreach($results as&$result)。这将是快速的,并使用最小的额外内存。我不知道他是否想取消他们或不这是一个很好的点!也许只需添加一个“重复指示器”,因为您已经有了引用。和你谈话很有趣。