jeudi 19 septembre 2019

How to Format the output of "->map(function ($data)" to create a json style format in PHP

I have the following table:

enter image description here

My main question is: How we can format

a function like ->map(function ($data)

in order to create outputs that take into consideration commercial designs of commercial API requesting curly braces, :, = , and other texts....

I tried the following code:

    $record['product_attributes'] = $this->productDetails->map(function ($data) {
        return $data['value'];
    })->toArray();

and returned:

"Screen Type": "Retina Display", "Front Facing Camera Megapixels": "7 megapixels",

Then I tried to edit the data returned:

$record['product_attributes'] = $this->productDetails->map(function ($data) {
    return $data['{'.'name'. ':' .'value'.'}'];
})->toArray();

but all values were returned as null:

product_attributes: null null null.....]

What I need is to format the output to display the following information:

 "product_attributes": [
 {
 "id": "EAN",
 "value_name": "123456789"
 },
 {
 "id": "BRAND",
 "value_name": "RAY BAN"
 }
 ],

were "id" in my case is "name" and "value_name" is "value"

any help appreciated.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2No4npV
via IFTTT

Aucun commentaire:

Enregistrer un commentaire