首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在laravel中返回唯一值

如何在laravel中返回唯一值
EN

Stack Overflow用户
提问于 2020-07-17 05:42:02
回答 2查看 126关注 0票数 3

这里我有这个样本数据,它基于类别产品返回,我需要限制重复的值。

原始JSON

代码语言:javascript
运行
复制
[{
        "brand": {
            "id": "fe877b45-8620-453a-8805-63f0cbd80752",
            "name": "No Brand",
            "slug": "no-brand",
            "description": "null"
        },
        "options": [{
                "id": "324af955-1aa9-42ea-be6e-bb4e5623a97a",
                "parent_id": "null",
                "name": "Need Insurance?"
            },
            {
                "id": "73298c18-4ccc-4138-afa5-71d3d00dff9b",
                "parent_id": "null",
                "name": "Color",
                "slug": "color"
            }
        ],
        "rating": [],
        "tags": [{
            "id": "8a31ee4c-3302-4357-9686-bd4308bbf39f",
            "name": "options",
            "slug": "options",
            "photo": "null"
        }],
        "variations": [{
                "id": "3bf5aeb9-9da2-4fb1-a3d2-f89eb75839c3",
                "parent_id": "null",
                "name": "Ram",
                "slug": "ram",
                "photo": "null"
            },
            {
                "id": "e5c70766-a558-4539-b41f-77f72c819a7c",
                "parent_id": "null",
                "name": "cpu",
                "slug": "cpu",
                "photo": "null"
            },
            {
                "id": "e63ac831-f595-4889-83d2-a5be65734758",
                "parent_id": "null",
                "name": "Monitor",
                "slug": "monitor"
            }
        ]
    },
    {
        "brand": {
            "id": "fe877b45-8620-453a-8805-63f0cbd80752",
            "name": "No Brand",
            "slug": "no-brand",
            "description": null
        },
        "options": [{
            "id": "522da418-eb3f-43e9-9392-63c941842a52",
            "parent_id": null,
            "name": "Color",
            "slug": "color-3"
        }],
        "rating": [],
        "tags": [],
        "variations": [{
            "id": "8e9a26c5-2ee4-4d86-9244-a10596d67fea",
            "parent_id": null,
            "name": "cpu",
            "slug": "cpu-3",
            "photo": null
        }]
    }
]

样本数据

代码语言:javascript
运行
复制
filters: [{,…}, {,…}]
    0: {,…}
        brand: {id: "fe877b45-8620-453a-8805-63f0cbd80752", name: "No Brand", slug: "no-brand", description: null,…}
        options: [{id: "324af955-1aa9-42ea-be6e-bb4e5623a97a", parent_id: null, name: "Need Insurance?",…},…]
            0: {id: "324af955-1aa9-42ea-be6e-bb4e5623a97a", parent_id: null, name: "Need Insurance?",…}
            1: {id: "73298c18-4ccc-4138-afa5-71d3d00dff9b", parent_id: null, name: "Color", slug: "color",…}
        rating: []
        tags: [{id: "8a31ee4c-3302-4357-9686-bd4308bbf39f", name: "options", slug: "options", photo: null,…},…]
        variations: [,…]
            0: {id: "3bf5aeb9-9da2-4fb1-a3d2-f89eb75839c3", parent_id: null, name: "Ram", slug: "ram", photo: null,…}
            1: {id: "e5c70766-a558-4539-b41f-77f72c819a7c", parent_id: null, name: "cpu", slug: "cpu", photo: null,…}
            2: {id: "e63ac831-f595-4889-83d2-a5be65734758", parent_id: null, name: "Monitor", slug: "monitor",…}
    1: {,…}
        brand: {id: "fe877b45-8620-453a-8805-63f0cbd80752", name: "No Brand", slug: "no-brand", description: null,…}
        options: [{id: "522da418-eb3f-43e9-9392-63c941842a52", parent_id: null, name: "Color", slug: "color-3",…}]
            0: {id: "522da418-eb3f-43e9-9392-63c941842a52", parent_id: null, name: "Color", slug: "color-3",…}
        rating: []
        tags: [,…]
        variations: [,…]
            0: {id: "8e9a26c5-2ee4-4d86-9244-a10596d67fea", parent_id: null, name: "cpu", slug: "cpu-3", photo: null,…}

解释

您可以看到,我的每个产品都有相同的数据字段,其中一些是相同的,例如,在options中,两个产品都有Color,或者在variations中都有cpubrand,两者都是相同的,在最后的结果中,我只需要有一个Color、一个cpu和一个品牌,因为它们是相同的。

代码

以上代码就是这样返回的。

代码语言:javascript
运行
复制
$data = [];
foreach($products as $i => $product) {
    $data[$i]['brand'] = $product->brand;
    $data[$i]['rating'] = $product->rating;
    $data[$i]['variations'] = $product->variations;
    $data[$i]['options'] = $product->options;
    $data[$i]['tags'] = $product->tags;
}

注意:最后的结果是将merge of all products data转换为single array,但是唯一的值。这就是我要找的。

有什么想法吗?

更新

最终结果的样本如下所示

代码语言:javascript
运行
复制
finalResult: [{,…}, {,…}]
    0: {,…}
        brand: {id: "fe877b45-8620-453a-8805-63f0cbd80752", name: "No Brand", slug: "no-brand", description: null,…}
            0: {id: "fe877b45-8620-453a-8805-63f0cbd80752", name: "No Brand", slug: "no-brand", description: null,…},…] // it was same in both products
        options: [{id: "324af955-1aa9-42ea-be6e-bb4e5623a97a", parent_id: null, name: "Need Insurance?",…},…]
            0: {id: "324af955-1aa9-42ea-be6e-bb4e5623a97a", parent_id: null, name: "Need Insurance?",…}
            1: {id: "73298c18-4ccc-4138-afa5-71d3d00dff9b", parent_id: null, name: "Color", slug: "color",…} // it was same in both products
        rating: []
        tags: [{id: "8a31ee4c-3302-4357-9686-bd4308bbf39f", name: "options", slug: "options", photo: null,…},…]
            0: {id: "8a31ee4c-3302-4357-9686-bd4308bbf39f", name: "options", slug: "options", photo: null,…}
            1: {id: "94ef99b6-ed2a-4eea-9248-e4775159eb58", name: "product", slug: "product", photo: null,…}
            2: {id: "378802b3-d13a-48c4-afa2-f9fed94d69ee", name: "werg", slug: "werg", photo: null, active: "yes",…} // added from another product
            3: {id: "f1380f50-af59-4f6a-8eca-d40689c1c1c1", name: "werwg", slug: "werwg", photo: null, active: "yes",…} // added from another product
        variations: [,…]
            0: {id: "3bf5aeb9-9da2-4fb1-a3d2-f89eb75839c3", parent_id: null, name: "Ram", slug: "ram", photo: null,…}
            1: {id: "e5c70766-a558-4539-b41f-77f72c819a7c", parent_id: null, name: "cpu", slug: "cpu", photo: null,…} // it was same in both products
            2: {id: "e63ac831-f595-4889-83d2-a5be65734758", parent_id: null, name: "Monitor", slug: "monitor",…}

更新2

获取数据的子数据

选项和变化都有孩子(即Cpu => [Core i7, Core i3])或color => ['red', 'black'],所以我需要收集这些孩子的孩子,并把他们放在父母的数据下。

Option model

代码语言:javascript
运行
复制
public function options()
    {
        return $this->hasMany(Option::class);
    }

    public function children() {
        return $this->hasMany(Option::class,'parent_id','id') ;
    }

    public function parent()
    {
        return $this->belongsTo(Option::class,'parent_id');
    }

    public function isParent()
    {
        return !$this->parent_id ? true : false; // if parent_id is null => is a Parent Option
    }

Variant model

代码语言:javascript
运行
复制
public function variants()
    {
        return $this->hasMany(Variant::class);
    }

    public function children() {
        return $this->hasMany(Variant::class,'parent_id','id') ;
    }

    public function parent()
    {
        return $this->belongsTo(Variant::class,'parent_id');
    }

    public function isParent()
    {
        return !$this->parent_id ? true : false; // if parent_id is null => is a Parent Variant
    }

注意:正如您可能理解的,父级和子级之间的关系由parent_id列定义。

Sample data (包括所有数组及其子程序)

代码语言:javascript
运行
复制
{
   "brands": [
      {
         "id": "fe877b45-8620-453a-8805-63f0cbd80752",
         "name": "no brand",
         "slug": "no-brand",
         "description": null,
         "photo": null,
         "created_at": "2020-07-15 11:35:18",
         "updated_at": "2020-07-15 11:35:18"
      }
   ],
   "options": [
      {
         "id": "324af955-1aa9-42ea-be6e-bb4e5623a97a",
         "parent_id": null,
         "name": "need insurance?",
         "slug": "need-insurance",
         "photo": null,
         "type": "radio",
         "active": "yes",
         "created_at": "2020-07-17 11:28:09",
         "updated_at": "2020-07-17 11:28:09",
         "pivot": {
            "product_id": "293c0369-04a7-4330-bb98-ede0bcf10f8d",
            "option_id": "324af955-1aa9-42ea-be6e-bb4e5623a97a"
         },
         "children": [
            {
               "id": "44afca9e-abf1-4a7a-9c46-d96d8127c2af",
               "parent_id": "324af955-1aa9-42ea-be6e-bb4e5623a97a",
               "name": "No",
               "slug": "no",
               "photo": null,
               "type": "radio",
               "active": "yes",
               "created_at": "2020-07-17 11:28:09",
               "updated_at": "2020-07-17 11:28:09"
            }
         ]
      },
      {
         "id": "73298c18-4ccc-4138-afa5-71d3d00dff9b",
         "parent_id": null,
         "name": "color",
         "slug": "color",
         "photo": null,
         "type": "dropdown",
         "active": "yes",
         "created_at": "2020-07-17 11:27:41",
         "updated_at": "2020-07-17 11:27:41",
         "pivot": {
            "product_id": "293c0369-04a7-4330-bb98-ede0bcf10f8d",
            "option_id": "73298c18-4ccc-4138-afa5-71d3d00dff9b"
         },
         "children": [
            {
               "id": "29b62f35-52a2-4a8b-ac8f-7e70e065488a",
               "parent_id": "73298c18-4ccc-4138-afa5-71d3d00dff9b",
               "name": "Black",
               "slug": "black",
               "photo": null,
               "type": "dropdown",
               "active": "yes",
               "created_at": "2020-07-17 11:27:41",
               "updated_at": "2020-07-17 11:27:41"
            },
            {
               "id": "4aa2d899-f1cc-4000-95e6-997d28dc51fc",
               "parent_id": "73298c18-4ccc-4138-afa5-71d3d00dff9b",
               "name": "Red",
               "slug": "red",
               "photo": null,
               "type": "dropdown",
               "active": "yes",
               "created_at": "2020-07-17 11:27:41",
               "updated_at": "2020-07-17 11:27:41"
            },
            {
               "id": "5f9de5bc-e966-48f3-b78c-de709dba86b5",
               "parent_id": "73298c18-4ccc-4138-afa5-71d3d00dff9b",
               "name": "Gray",
               "slug": "gray",
               "photo": null,
               "type": "dropdown",
               "active": "yes",
               "created_at": "2020-07-17 11:27:41",
               "updated_at": "2020-07-17 11:27:41"
            },
            {
               "id": "f248d6ab-1b88-4ea4-8d6a-62fe271bfa8a",
               "parent_id": "73298c18-4ccc-4138-afa5-71d3d00dff9b",
               "name": "White",
               "slug": "white",
               "photo": null,
               "type": "dropdown",
               "active": "yes",
               "created_at": "2020-07-17 11:27:41",
               "updated_at": "2020-07-17 11:27:41"
            }
         ]
      },
      {
         "id": "522da418-eb3f-43e9-9392-63c941842a52",
         "parent_id": null,
         "name": "color",
         "slug": "color-3",
         "photo": null,
         "type": "radio",
         "active": "yes",
         "created_at": "2020-07-17 12:20:46",
         "updated_at": "2020-07-17 12:20:46",
         "pivot": {
            "product_id": "a8bb27c8-e968-4317-b4d2-8e5cd6049ff8",
            "option_id": "522da418-eb3f-43e9-9392-63c941842a52"
         },
         "children": [
            {
               "id": "84135f25-690b-407b-8c98-e7526429a594",
               "parent_id": "522da418-eb3f-43e9-9392-63c941842a52",
               "name": "Red",
               "slug": "red-3",
               "photo": null,
               "type": "radio",
               "active": "yes",
               "created_at": "2020-07-17 12:20:46",
               "updated_at": "2020-07-17 12:20:46"
            },
            {
               "id": "9d1f0d9c-272a-4e96-ac0a-aeac869bfc30",
               "parent_id": "522da418-eb3f-43e9-9392-63c941842a52",
               "name": "Yellow",
               "slug": "yellow-2",
               "photo": null,
               "type": "radio",
               "active": "yes",
               "created_at": "2020-07-17 12:20:46",
               "updated_at": "2020-07-17 12:20:46"
            }
         ]
      }
   ],
   "ratings": [
      "4.5",
      "4.0"
   ],
   "tags": [
      {
         "id": "8a31ee4c-3302-4357-9686-bd4308bbf39f",
         "name": "options",
         "slug": "options",
         "photo": null,
         "active": "yes",
         "created_at": "2020-07-17 11:29:47",
         "updated_at": "2020-07-17 11:29:47",
         "pivot": {
            "product_id": "293c0369-04a7-4330-bb98-ede0bcf10f8d",
            "tag_id": "8a31ee4c-3302-4357-9686-bd4308bbf39f"
         }
      },
      {
         "id": "94ef99b6-ed2a-4eea-9248-e4775159eb58",
         "name": "product",
         "slug": "product",
         "photo": null,
         "active": "yes",
         "created_at": "2020-07-17 11:29:47",
         "updated_at": "2020-07-17 11:29:47",
         "pivot": {
            "product_id": "293c0369-04a7-4330-bb98-ede0bcf10f8d",
            "tag_id": "94ef99b6-ed2a-4eea-9248-e4775159eb58"
         }
      },
      {
         "id": "378802b3-d13a-48c4-afa2-f9fed94d69ee",
         "name": "werg",
         "slug": "werg",
         "photo": null,
         "active": "yes",
         "created_at": "2020-07-15 11:53:13",
         "updated_at": "2020-07-15 11:53:13",
         "pivot": {
            "product_id": "a8bb27c8-e968-4317-b4d2-8e5cd6049ff8",
            "tag_id": "378802b3-d13a-48c4-afa2-f9fed94d69ee"
         }
      },
      {
         "id": "f1380f50-af59-4f6a-8eca-d40689c1c1c1",
         "name": "werwg",
         "slug": "werwg",
         "photo": null,
         "active": "yes",
         "created_at": "2020-07-15 11:53:13",
         "updated_at": "2020-07-15 11:53:13",
         "pivot": {
            "product_id": "a8bb27c8-e968-4317-b4d2-8e5cd6049ff8",
            "tag_id": "f1380f50-af59-4f6a-8eca-d40689c1c1c1"
         }
      }
   ],
   "variations": [
      {
         "id": "3bf5aeb9-9da2-4fb1-a3d2-f89eb75839c3",
         "parent_id": null,
         "name": "ram",
         "slug": "ram",
         "photo": null,
         "type": "input",
         "active": "yes",
         "created_at": "2020-07-17 11:27:05",
         "updated_at": "2020-07-17 11:27:05",
         "pivot": {
            "product_id": "293c0369-04a7-4330-bb98-ede0bcf10f8d",
            "variant_id": "3bf5aeb9-9da2-4fb1-a3d2-f89eb75839c3"
         },
         "children": [
            {
               "id": "5687d6a8-12df-41b2-bf2f-b822faae8af0",
               "parent_id": "3bf5aeb9-9da2-4fb1-a3d2-f89eb75839c3",
               "name": "4 Gig",
               "slug": "4 Gig",
               "photo": null,
               "type": "input",
               "active": "yes",
               "created_at": "2020-07-17 11:27:05",
               "updated_at": "2020-07-17 11:27:05"
            }
         ]
      },
      {
         "id": "e5c70766-a558-4539-b41f-77f72c819a7c",
         "parent_id": null,
         "name": "cpu",
         "slug": "cpu",
         "photo": null,
         "type": "input",
         "active": "yes",
         "created_at": "2020-07-17 11:26:58",
         "updated_at": "2020-07-17 11:26:58",
         "pivot": {
            "product_id": "293c0369-04a7-4330-bb98-ede0bcf10f8d",
            "variant_id": "e5c70766-a558-4539-b41f-77f72c819a7c"
         },
         "children": [
            {
               "id": "83003a24-cc69-4305-8d3a-e99da91d3354",
               "parent_id": "e5c70766-a558-4539-b41f-77f72c819a7c",
               "name": "Core i7",
               "slug": "Core i7",
               "photo": null,
               "type": "input",
               "active": "yes",
               "created_at": "2020-07-17 11:26:58",
               "updated_at": "2020-07-17 11:26:58"
            }
         ]
      },
      {
         "id": "e63ac831-f595-4889-83d2-a5be65734758",
         "parent_id": null,
         "name": "monitor",
         "slug": "monitor",
         "photo": null,
         "type": "input",
         "active": "yes",
         "created_at": "2020-07-17 11:27:21",
         "updated_at": "2020-07-17 11:27:21",
         "pivot": {
            "product_id": "293c0369-04a7-4330-bb98-ede0bcf10f8d",
            "variant_id": "e63ac831-f595-4889-83d2-a5be65734758"
         },
         "children": [
            {
               "id": "816e1fab-24eb-49e9-9b3a-d4b4cce16cdf",
               "parent_id": "e63ac831-f595-4889-83d2-a5be65734758",
               "name": "14\"",
               "slug": "14\"",
               "photo": null,
               "type": "input",
               "active": "yes",
               "created_at": "2020-07-17 11:27:21",
               "updated_at": "2020-07-17 11:27:21"
            }
         ]
      },
      {
         "id": "8e9a26c5-2ee4-4d86-9244-a10596d67fea",
         "parent_id": null,
         "name": "cpu",
         "slug": "cpu-3",
         "photo": null,
         "type": "input",
         "active": "yes",
         "created_at": "2020-07-17 12:20:56",
         "updated_at": "2020-07-17 12:20:56",
         "pivot": {
            "product_id": "a8bb27c8-e968-4317-b4d2-8e5cd6049ff8",
            "variant_id": "8e9a26c5-2ee4-4d86-9244-a10596d67fea"
         },
         "children": [
            {
               "id": "50857808-106e-4ae0-8c02-a54761e6dac7",
               "parent_id": "8e9a26c5-2ee4-4d86-9244-a10596d67fea",
               "name": "Core i3",
               "slug": "Core i3-2",
               "photo": null,
               "type": "input",
               "active": "yes",
               "created_at": "2020-07-17 12:20:56",
               "updated_at": "2020-07-17 12:20:56"
            }
         ]
      }
   ]
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-07-17 06:54:42

您可以在数据的一次迭代中实现您想要的结果,可以使用如下方法:

代码语言:javascript
运行
复制
$variations = [];

$result = array_reduce($filters, function ($result, $filter) use ($variations) {
    $filter['brand']['name'] = strtolower($filter['brand']['name']);
    if ($result['brands']->where('name', $filter['brand']['name'])->isEmpty()) {
        $result['brands']->push($filter['brand']);
    }

    foreach ($filter['options'] as $option) {
        $option['name'] = strtolower($option['name']);

        if ($result['options']->where('name', $option['name'])->isEmpty()) {
            $result['options']->push($option);
        }
    }

    if (isset($filter['rating']['id'])) {
        if ($result['ratings']->where('id', $filter['rating']['id'])->isEmpty()) {
            $result['ratings']->push($filter['rating']);
        }
    }

    foreach ($filter['tags'] as $tag) {
        $tag['name'] = strtolower($tag['name']);

        if ($result['tags']->where('name', $tag['name'])->isEmpty()) {
            $result['tags']->push($tag);
        }
    }

    foreach ($filter['variations'] as $variation) {
        $variation['name'] = strtolower($variation['name']);
        $variationName = $variation['name'];         
        $children = collect($variation['children'])->pluck('name');

        if ($result['variations']->where('name', $variation['name'])->isEmpty()) {
            $result['variations']->push($variation);
            $variations[$variationName] = $children;

        } else {
            $different = $variations[$variationName]->diff($children);
            
            if ($different->isNotEmpty()) {
               $result['variations']->push($variation);
               foreach ($different as $childName) {
                   $variations[$variationName]->push($childName);
               }  
            }
        }
    }

    return $result;

}, collect([
    'brands' => collect(),
    'options' => collect(),
    'ratings' => collect(),
    'tags' => collect(),
    'variations' => collect()
]));

如果需要将结果作为数组,可以使用集合的toArray方法:

代码语言:javascript
运行
复制
    $result->toArray();
票数 4
EN

Stack Overflow用户

发布于 2020-07-17 06:34:00

我已经根据您的输入添加了带有行json数据的示例代码。

代码语言:javascript
运行
复制
$jsonData = '[{
"brand": {"id": "fe877b45-8620-453a-8805-63f0cbd80752", "name": "No Brand", "slug": "no-brand", "description": "null"},
"options": [{"id": "324af955-1aa9-42ea-be6e-bb4e5623a97a", "parent_id": "null", "name": "Need Insurance?"},
{"id": "73298c18-4ccc-4138-afa5-71d3d00dff9b", "parent_id": "null", "name": "Color", "slug": "color"}],
"rating": [],
"tags": [{"id": "8a31ee4c-3302-4357-9686-bd4308bbf39f", "name": "options", "slug": "options", "photo": "null"}],
"variations": [{"id": "3bf5aeb9-9da2-4fb1-a3d2-f89eb75839c3", "parent_id": "null", "name": "Ram", "slug": "ram", "photo": "null"},
{"id": "e5c70766-a558-4539-b41f-77f72c819a7c", "parent_id": "null", "name": "cpu", "slug": "cpu", "photo": "null"},
{"id": "e63ac831-f595-4889-83d2-a5be65734758", "parent_id": "null", "name": "Monitor", "slug": "monitor"}
]
},
{
"brand": {"id": "fe877b45-8620-453a-8805-63f0cbd80752", "name": "No Brand", "slug": "no-brand", "description": null},
"options": [{"id": "522da418-eb3f-43e9-9392-63c941842a52", "parent_id": null, "name": "Color", "slug": "color-3"}],
"rating": [],
"tags": [],
"variations": [{"id": "8e9a26c5-2ee4-4d86-9244-a10596d67fea", "parent_id": null, "name": "Cpu", "slug": "cpu-3", "photo": null}]
}
]
';


$jsonDataArr =json_decode($jsonData);
$data = array();

foreach($jsonDataArr as $key => $items){
    
    foreach($items as $innerKey => $eachItem){
        if(!isset($data[$innerKey])){
            $data[$innerKey] = array();
            if(is_array($eachItem)){
                
                foreach($eachItem as $each)
                    if(!empty($each))
                        $data[$innerKey][] = (array) $each; 
                
            }else{
            
                if(!empty($eachItem))
                $data[$innerKey][] = (array) $eachItem; 
            
            }
        }else{
            if(is_array($eachItem)){
                foreach($eachItem as $each)
                    if(!empty($each))
                        $data[$innerKey][] = (array) $each;     
            }else{
            
                if(!empty($eachItem))
                $data[$innerKey][] = (array) $eachItem; 
            
            }
        }
    }
}

foreach($data as $key => $val){
    
    foreach($val as $l => $item){
        $index = trim(strtolower($item['name']));
        $data[$key][$index] = $item;
        unset($data[$key][$l]);
    }
    
}

foreach($data as $key => $val){
    $data[$key]=array_values($val); 
}

print_r($data);

演示

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62947795

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档