Attributes Generation

2 years ago

Chris1904

Code

$collection = collect([
    [
        "Background" => "Layer-1",
        "value" => "Static",
    ],
    [
        "Eyes" => "Layer-2",
        "value" => "Up and down",
    ],
    [
        "Mouth" => "Layer-3",
        "value" => "British",
    ],
    [
        "Hair" => "Layer-4",
        "value" => "Pherb",
    ],
    [
        "Eyebrows" => "Layer-5",
        "value" => "Little Sad",
    ],
    [
        "Glasses" => "Layer-6",
        "value" => "none",
    ],
]);

$goal= [];

if (array_key_first($collection->first()) !== 'trait_type') {
    $goal = $collection->map(fn ($attribute) => [array_key_first($attribute) => $attribute['value']])
                    ->toArray();
  
  dd(array_merge(...$goal));
}

Output

array:6 [
  "Background" => "Static"
  "Eyes" => "Up and down"
  "Mouth" => "British"
  "Hair" => "Pherb"
  "Eyebrows" => "Little Sad"
  "Glasses" => "none"
]
Video course: PHP Package Development