繁体   English   中英

从 Laravel object 构建 PHP 多维数组

[英]construct PHP multidimensional array from Laravel object

这是来自 Laravel DB 的数据列表示例:

Illuminate\Support\Collection Object
(
[items:protected] => Array
    (
        [0] => stdClass Object
            (
                [category] => Protections menstruelles
                [sub_category] => Sous-vêtements lavables
                [model] => Culotte menstruelle
            )

        [1] => stdClass Object
            (
                [category] => T-shirt, Top & Polo
                [sub_category] => Débardeur, Caraco
                [model] => Bustier
            )

        [2] => stdClass Object
            (
                [category] => Protections menstruelles
                [sub_category] => Sous-vêtements lavables
                [model] => Tanga menstruel
            )

        [3] => stdClass Object
            (
                [category] => Protections menstruelles
                [sub_category] => Sous-vêtements lavables
                [model] => Body menstruel
            )

        [4] => stdClass Object
            (
                [category] => Protections menstruelles
                [sub_category] => Maillot de bain menstruel
                [model] => 1 pièce
            )

        [5] => stdClass Object
            (
                [category] => Homewear
                [sub_category] => Chaussettes
                [model] => Socquettes
            )

        [6] => stdClass Object
            (
                [category] => Plage
                [sub_category] => Bas de maillot
                [model] => Short de bain
            )

        [7] => stdClass Object
            (
                [category] => Lingerie & Sous-vêtement
                [sub_category] => Soutien-gorge
                [model] => Triangle
            )

        [8] => stdClass Object
            (
                [category] => Lingerie & Sous-vêtement
                [sub_category] => Culotte, tanga & string
                [model] => Tanga
            )

        [9] => stdClass Object
            (
                [category] => Lingerie & Sous-vêtement
                [sub_category] => Culotte, tanga & string
                [model] => Culotte
            )

    )
)

我想创建一个 JSON object 可能是这样的:

        var subjectObject = {
          "Protections menstruelles": {
            "Maillot de bain menstruel": ["1 pièce"],
            "Sous-vêtements lavables": ["Body menstruel"]
          },
          "Lingerie & Sous-vêtement": {
            "Soutien-gorge": ["Triangle"],
            "Culotte, tanga & string": ["Tanga", "Culotte"]
          }
          ...
        }

我的想法是在 select 和 JavaScript 中提出一些问题,条件是根据之前的 select 的选项。我想为此使用本教程:https:https://www.w3schools.com/howto/howto_js_cascading_dropdown.asp我需要帮助来构建json 输入。

它正在处理这个 laravel 数据库请求:

->select('category','sub_category','model')
->whereNotNull('category')
->whereNotNull('sub_category')
->whereNotNull('model')
->distinct()
->get()
->groupBy('category')
->map(function($item){
    return collect($item)
        ->groupBy('sub_category')
        ->map(function($item){
            return collect($item)->pluck('model');
        })->toArray();
      })->toJson();

但我不能在 javascript 中使用它:

var subjectObject = "{{ $collection }}";

它不是示例中的 json:

var subjectObject = {
 "Front-end": {
  "HTML": ["Links", "Images", "Tables", "Lists"],
  "CSS": ["Borders", "Margins", "Backgrounds", "Float"],
  "JavaScript": ["Variables", "Operators", "Functions", "Conditions"]    
 },
 "Back-end": {
   "PHP": ["Variables", "Strings", "Arrays"],
   "SQL": ["SELECT", "UPDATE", "DELETE"]
 }
}

这是一个集合。 您可以使用这些方法:

  • groupBy方法按给定键对集合的项目进行分组
  • map方法遍历集合并将每个值传递给给定的回调
  • pluck方法检索给定键的所有值,并且
  • toJson方法将集合转化为JSON序列化字符串
$collection
    ->groupBy('category')
    ->map(function($item){
        return collect($item)
            ->groupBy('sub_category')
            ->map(function($item){
                return collect($item)->pluck('model');
            })
            ->toArray();
    })
    ->toJson();

Output:

{
  "Protections menstruelles": {
    "Sous-vêtements lavables": [
      "Culotte menstruelle",
      "Tanga menstruel",
      "Body menstruel"
    ],
    "Maillot de bain menstruel": [
      "1 pièce"
    ]
  },
  "T-shirt, Top & Polo": {
    "Débardeur, Caraco": [
      "Bustier"
    ]
  },
  "Homewear": {
    "Chaussettes": [
      "Socquettes"
    ]
  },
  "Plage": {
    "Bas de maillot": [
      "Short de bain"
    ]
  },
  "Lingerie & Sous-vêtement": {
    "Soutien-gorge": [
      "Triangle"
    ],
    "Culotte, tanga & string": [
      "Tanga",
      "Culotte"
    ]
  }
}

暂无
暂无

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

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