Displaying keys and values in a multidimensional Laravel object

I would like to put my keys and values in a collection like this

$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'],
        ];

I also have the values I receive in the database, saved in a json
Like this
 "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"}]}"

I tried to make something like this
$attributes = [];

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

But I will receive something this way
IlluminateSupportCollection {#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"
      }
    ]
  ]

In the end, I would like to somehow push that collection and have the key, value as I have above
Is something like that possible?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Hi You can try like this

 $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);

Desire Results
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"
  ]
]


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x