【问题标题】:Displaying keys and values in a multidimensional Laravel object在多维 Laravel 对象中显示键和值
【发布时间】:2021-11-10 07:52:26
【问题描述】:

我想将我的键和值放在这样的集合中

$attributes = [
            ['key' => 'color', 'value' => 'red'],
            ['key' => 'color', 'value' => 'blue'],
            ['key' => 'color', 'value' => 'red'],
            ['key' => 'height', 'value' => (int) 2],
            ['key' => 'height', 'value' => (int) 2],
            ['key' => 'height', 'value' => (int) 3],
            ['key' => 'height', 'value' => (int) 3],
            ['key' => 'height', 'value' => (int) 3],
            ['key' => 'height', 'value' => (int) 3],
            ['key' => 'height', 'value' => (int) 3],
            ['key' => 'color', 'value' => 'red'],
            ['key' => 'material', 'value' => 'iron'],
            ['key' => 'material', 'value' => 'iron'],
            ['key' => 'material', 'value' => 'gold'],
            ['key' => 'material', 'value' => 'gold'],
            ['key' => 'material', 'value' => 'gold'],
        ];

我也有我在数据库中收到的值,保存在一个 json 像这样

 "attributes" => "{"Attribute":[{"Attribute_name":"heigth","Attribute_value":"8"},{"Attribute_name":"color","Attribute_value":"red"},{"Attribute_name":"heigth","Attribute_value":"8"},{"Attribute_name":"heigth","Attribute_value":"2"}]}"

我试着做这样的事情

$attributes = [];

        foreach ($feeds as $feed) {
            array_push($attributes, $feed->attributes);
        }
        $test = collect(json_decode($attributes[0]));
        dd($test);

但我会以这种方式收到一些东西

Illuminate\Support\Collection {#696
  #items: array:1 [
    "Attribute" => array:4 [
      0 => {#380
        +"Attribute_name": "height"
        +"Attribute_value": "8"
      }
      1 => {#373
        +"Attribute_name": "color"
        +"Attribute_value": "red"
      }
      2 => {#359
        +"Attribute_name": "height"
        +"Attribute_value": "8"
      }
      3 => {#1315
        +"Attribute_name": "height"
        +"Attribute_value": "2"
      }
    ]
  ]

最后,我想以某种方式推送该集合并拥有上面的键值 有这样的可能吗?

【问题讨论】:

    标签: php laravel collections


    【解决方案1】:

    你好,你可以这样试试

     $attributes = '{"Attribute":[{"Attribute_name":"heigth","Attribute_value":"8"},{"Attribute_name":"color","Attribute_value":"red"},{"Attribute_name":"heigth","Attribute_value":"8"},{"Attribute_name":"heigth","Attribute_value":"2"}]}';
          $attributes_array = json_decode($attributes);
    
          $attributes = [];
          foreach ($attributes_array->Attribute as $key=> $value){ 
              $attributes[$key]['key']=$value->Attribute_name;
              $attributes[$key]['value']=$value->Attribute_value;
          }
          dd($attributes);
    

    渴望结果

    array:4 [▼
      0 => array:2 [▼
        "key" => "heigth"
        "value" => "8"
      ]
      1 => array:2 [▼
        "key" => "color"
        "value" => "red"
      ]
      2 => array:2 [▼
        "key" => "heigth"
        "value" => "8"
      ]
      3 => array:2 [▼
        "key" => "heigth"
        "value" => "2"
      ]
    ]
    

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 2021-04-23
      • 1970-01-01
      • 1970-01-01
      • 2015-12-30
      • 1970-01-01
      • 2021-09-25
      • 2019-07-23
      • 1970-01-01
      相关资源
      最近更新 更多