简体   繁体   English

没有SQL查询如何获取唯一数据

[英]How can I get unique data without sql query

I'm updating brand details in DB. 我正在更新数据库中的品牌详细信息。 But I'm getting multiple entries of the same name. 但是我得到了多个同名条目。
When running the following line: 运行以下行时:

$brand = $brands->brand;

The result of this $brand is 这个$brand的结果是

puma,nike,puma,nike,puma,nike 彪马,耐克,彪马,耐克,彪马,耐克

I only want unique brand names, how can I do that? 我只想要唯一的品牌名称,该怎么办?

I'm not super familiair with Laravel but the general idea is: 我不是Laravel的超级家族,但总体思路是:

Create an array . 创建一个array For every name you get, check if it's already in the array, if not, add it to the array and process it. 对于获得的每个名称,请检查它是否已存在于数组中,如果没有,则将其添加到数组中并进行处理。 If it is already in the array, skip it. 如果它已经在数组中,请跳过它。

PHP example, not sure for Laravel PHP示例,不确定Laravel

$all_brands = array();
// Check if the brand is not already in the array
if(!in_array($brand, $all_brands)) {
    $all_brands[] = $brand;
    // Process the brand
} else {
    // Ignore entry
}

PHP manual: in_array function PHP手册:in_array函数

you receive this number of brands because when you extract the brand from dataBase you extract every brand for every product. 之所以会收到这个品牌数,是因为从数据库中提取品牌时,您会提取每种产品的每个品牌。 Try to extract just once. 尝试仅提取一次。

Try, if 尝试,如果

$brands = ["puma","nike","puma","nike","puma","nike"];

then 然后

$uniqueBrands = array_unique($brands);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM